hello les amis
J'utilise SQL Server 8
je veux faire un join entre une table et le résultat d'un union. Je croyais que ma requete fonctionnait à merveille jusqu'à ce matin où je me suis rendue compte d'un cas d'exception qu'il me faut régler.
Je vous montre mon code SQL.
Les deux tables qui sont jointes sont budgetfin et celle ayant comme alias t_pr_cs.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 declare @marque as varchar(3) declare @format as varchar(15) declare @groupe as varchar(3) declare @devin as varchar(5) declare @annee as int declare @sem_cour as int set @marque = 'OAN' set @format = 'S24X236ML' set @groupe = 'SUP' set @devin = '3053' set @annee = 2004 set @sem_cour = 30 select b.semaine, (case when t_pr_cs.pr_cs IS NULL then 0 else t_pr_cs.pr_cs end) as "PR", (case when sum(de_cs) IS NULL then 0 else sum(de_cs) end) as "DE" from budgetfin b inner join ( select semaine, sum(pr_cs) as "pr_cs" from budgetfin bf2 where semaine > @sem_cour and annee = @annee and noclientdevin = @devin and nomarque = @marque and noformat = @format and nogroupe = @groupe and type = 'VENTES' group by semaine UNION select semaine, sum(pr_qte) as "pr_cs" from statsvente join lassonde.dbo.iim on (iprod = noproduit) where statsvente.semaine <= @sem_cour and statsvente.annee = @annee and noclientdevin = @devin and imarqu = @marque and iforma = @format and igroup = @groupe group by semaine ) as t_pr_cs on b.semaine = t_pr_cs.semaine where b.annee = @annee and b.noclientdevin = @devin and b.nomarque = @marque and b.noformat = @format and b.nogroupe = @groupe and b.type = 'VENTES' group by b.semaine, t_pr_cs.pr_cs order by b.semaine
Mais voila, je me rend compte que si budgetfin ne possede pas d'enregistrement pour une semaine X, cette semaine la n'apparait pas dans le résultat final, meme si t_pr_cs possede des valeurs pour cette semaine la.
Comment je sais cela?
Parce que la section t_pr_cs
me renvoie ceci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 select semaine, sum(pr_cs) as "pr_cs" from budgetfin bf2 where semaine > @sem_cour and annee = @annee and noclientdevin = @devin and nomarque = @marque and noformat = @format and nogroupe = @groupe and type = 'VENTES' group by semaine UNION select semaine, sum(pr_qte) as "pr_cs" from statsvente join lassonde.dbo.iim on (iprod = noproduit) where statsvente.semaine <= @sem_cour and statsvente.annee = @annee and noclientdevin = @devin and imarqu = @marque and iforma = @format and igroup = @groupe group by semaine
Tandis que la section budgetfinsemaine pr_cs
------- -----------
24 1422
32 0
(2 row(s) affected)
me renvoie ceci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 select b.semaine, (case when sum(de_cs) IS NULL then 0 else sum(de_cs) end) as "DE" from budgetfin b where b.annee = @annee and b.noclientdevin = @devin and b.nomarque = @marque and b.noformat = @format and b.nogroupe = @groupe and b.type = 'VENTES' group by b.semaine order by b.semaine
Mais la requête au complet me renvoie cecisemaine DE
------- -----------
32 0
(1 row(s) affected)
Vous savez comment je pourrais modifier ma requête pour que je puisse obtenir mon 1422 dans le résultat de la requête finale?semaine PR DE
------- ----------- -----------
32 0 0
(1 row(s) affected)
Merci beaucoup!
Partager