Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with a query using (WITH clause)

$
0
0

Hi there,

I'm hoping someone can help me with this. Basically the query I have is working great when there is some data in the Business_Statistics table but when there are no rows I'm not getting any data back. With the LEFT OUTER join I was hoping to still get all the dates in the date range but nulls (converted to zero) for the values but instead I am just getting zero rows.

So for example even with zero rows in the Business_Statistics table I would still expect:

Date   Play Count
Jan-15      0
Jan-16      0
Jan-17     0

etc.

Can anyone tell me what I'm doing wrong?

Thanks!

WITH dates(Date) AS 
(
    SELECT Cast('2014-01-05' as Date) as Date
    UNION ALL
    SELECT DATEADD(d,1,[Date])
    FROM dates 
    WHERE DATE < '2014-02-04'
)

select LEFT(REPLACE(CONVERT(VARCHAR(9), b.Date, 6), ' ', '-'),6) AS 'Date',
sum(ISNULL(PlayerPlay,0)) as 'Play Count'
from (

SELECT Date
FROM dates

) b left outer join 
Business_DigitalStatistic a on CAST(a.FirstAction AS DATE) = b.Date

where ClientID = 74  

group by b.Date
order by b.Date asc

OPTION (MAXRECURSION 0)


Olly


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>