Précédent   Forum des professionnels en informatique > Bases de données > Firebird > SQL
SQL Forum d'entraide sur le SQL pour Firebird
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 16/08/2011, 10h40   #1
Membre Expert
 
Homme AbdelHakim Kellouche
Inscription : mai 2008
Messages : 1 977
Détails du profil
Informations personnelles :
Nom : Homme AbdelHakim Kellouche
Localisation : Algérie

Informations professionnelles :
Secteur : Service public

Informations forums :
Inscription : mai 2008
Messages : 1 977
Points : 2 126
Points : 2 126
Envoyer un message via MSN à Just-Soft Envoyer un message via Skype™ à Just-Soft
Par défaut FB 2.5, un vrai casse-tête avec coalesce

Salut !

afin d'évaluer une expression qui peut contenir des données "null" j'ai utilisé la fonction coalesce mais l'utilisation de cette fonction me renvoie toujours "null" même si j'ai des lignes qui ne contiennent pas de membres "null".

Je copie ma PS toute entière, j'en ai fais deux versions avec et sans coalesce.

avec coalesce :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
CREATE PROCEDURE PS_RATIO_CENTRE_DRAIDJ (
    date_debut date,
    date_fin date)
returns (
    code_unite varchar(2),
    nom_unite varchar(20),
    frais_centre bigint,
    frais_secteur bigint,
    volume_collecte bigint,
    ratio_exp_eau_collectee float)
as
BEGIN
FOR
    select
    c.code_unite,
    c.nom_unite,
    sum( a.frais ) frais_centre,

    avg(
        (   select sum(tb_frais_exploitations.frais) from tb_frais_exploitations
            inner join tb_secteur on (tb_frais_exploitations.code_secteur = tb_secteur.code_secteur)
            inner join tb_unite on (tb_unite.code_unite = tb_secteur.code_unite)
            where (         ( tb_frais_exploitations.date_mois = a.date_mois )
                        and (tb_unite.code_unite = c.code_unite)
                  )
            ) )
        frais_secteur,

    (select sum(tb_curage.volume_collecte) from tb_curage
        inner join tb_commune on (tb_commune.code_commune = tb_curage.code_commune)
        inner join tb_centre on (tb_centre.code_centre = tb_commune.code_centre)
        inner join tb_unite on (tb_unite.code_unite = tb_centre.code_unite)
    where (  tb_curage.date_curage between :date_debut and :date_fin
             and (tb_unite.code_unite = c.code_unite)
    )

    ) volume_,

    ( round(cast ( sum ( a.frais ) + avg (
                 (   select coalesce(0, sum(tb_frais_exploitations.frais)) from tb_frais_exploitations
            inner join tb_secteur on (tb_frais_exploitations.code_secteur = tb_secteur.code_secteur)
            inner join tb_centre on (tb_centre.code_centre = tb_secteur.code_centre)
            inner join tb_unite on (tb_unite.code_unite = tb_centre.code_unite)
            where (         ( tb_frais_exploitations.date_mois = a.date_mois )
                        and (tb_unite.code_unite = c.code_unite)
                  )
         )

    ) as numeric (6,3)  ) / (select sum(tb_curage.volume_collecte) from tb_curage
        inner join tb_commune on (tb_commune.code_commune = tb_curage.code_commune)
        inner join tb_centre on (tb_centre.code_centre = tb_commune.code_centre)
        inner join tb_unite on (tb_unite.code_unite = tb_centre.code_unite)
    where (  tb_curage.date_curage between :date_debut and :date_fin

             and (tb_unite.code_unite = c.code_unite)
    ) ) ,2) ) ratio_

    from tb_frais_exploitations a
        inner join tb_centre b on (a.code_centre = b.code_centre)
        inner join tb_unite c on (b.code_unite = c.code_unite)
    where
        (
            (a.date_mois between :date_debut and :date_fin)
        )

    group by c.code_unite, c.nom_unite

    into    :code_unite,
            :nom_unite,
            :frais_centre,
            :frais_secteur,
            :volume_collecte,
            :ratio_exp_eau_collectee
            
do
begin
  suspend;
end
end
avec coalesce : (je donne la partie modifiée seulement)
Code :
1
2
3
4
5
6
7
8
9
round(cast ( sum ( a.frais ) + avg (
                 (   select sum(tb_frais_exploitations.frais) from tb_frais_exploitations
            inner join tb_secteur on (tb_frais_exploitations.code_secteur = tb_secteur.code_secteur)
            inner join tb_centre on (tb_centre.code_centre = tb_secteur.code_centre)
            inner join tb_unite on (tb_unite.code_unite = tb_centre.code_unite)
            where (         ( tb_frais_exploitations.date_mois = a.date_mois )
                        and (tb_unite.code_unite = c.code_unite)
                  )
         )
j'ai retiré le coalesce de la partie soulignée rouge.

Voici en pièce-jointe les résultats des deux versions. je voudrais bien utiliser la fonction coalesce mais sans que celle-ci n'influe sur les lignes qui n'ont pas de membres "null" et que mon ratio sois calculé quelque soit l'expression à retenir.

Merci par avance.
Fichiers attachés
Type de fichier : xls ratio.xls (21,5 Ko, 0 affichages)
__________________
Bon courage ou Bonne Chance (selon le contexte)
Just-Soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/08/2011, 16h38   #2
Membre Expert
 
Homme AbdelHakim Kellouche
Inscription : mai 2008
Messages : 1 977
Détails du profil
Informations personnelles :
Nom : Homme AbdelHakim Kellouche
Localisation : Algérie

Informations professionnelles :
Secteur : Service public

Informations forums :
Inscription : mai 2008
Messages : 1 977
Points : 2 126
Points : 2 126
Envoyer un message via MSN à Just-Soft Envoyer un message via Skype™ à Just-Soft
Le problème venait de l'ordre des arguments. Fallait mettre :
Code :
coalesce(sum(tb_frais_exploitations.frais), 0)
__________________
Bon courage ou Bonne Chance (selon le contexte)
Just-Soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 09h17.


 
 
 
 
Partenaires

Hébergement Web