1
declare @customerID int
set @customerID=1
;with cteDates as
(
select dateadd(hh, datediff(hh, 0, getdate()), 0) as [Date]
union all
select dateadd(hh, -1, [Date]) as [Date]
from cteDates
where [Date] > dateadd(hh, -23, getdate())
)
select
sum(coalesce(field1,0)) field1,
sum(coalesce(field2,0)) field2,
sum(coalesce(field3,0)) field3,
sum(coalesce(field4,0)) field4,
sum(coalesce(field5,0)) field5,
d.[Date]
from cteDates as d left join [Statistics] s
on d.Date=s.date
where [email protected] and s.date>dateadd(hh,-24,getdate())
group by d.Date
Je veux obtenir des statistiques horaires pour certains clients, s'il n'y a pas de valeur que je veux sélectionner 0 quand même ...Pourquoi cette requête tsql ne sélectionne pas ce qu'elle voulait?
Cette requête sélectionne uniquement les lignes qui existe dans le tableau statistique, même si je l'ai laissé se joindre à CTE ... calendrier
grâce ...
merci beaucoup !!! l33t – eugeneK
aucun problème .... –
http://wiki.lessthandot.com/index.php/WHERE_conditions_on_a_LEFT_JOIN – HLGEM