I have the following SQL Query:
SELECT SUM(data.frequency) as total,
data.day as day,
data.weekday,
CASE data.weekday
WHEN 1 THEN 'SUN'
WHEN 2 THEN 'MON'
WHEN 3 THEN 'TUE'
WHEN 4 THEN 'WED'
WHEN 5 THEN 'THU'
WHEN 6 THEN 'FRI'
WHEN 7 THEN 'SAT'
END as weekday
FROM (
SELECT COUNT(createdate) as frequency,
DATEPART(day,createdate) as day,
DATEPART(weekday,createdate) as weekday,
createdate as createddate
FROM requests ma
WHERE createdate BETWEEN
@startdate
AND
@enddate
GROUP BY datepart(day,createdate),DATEPART(weekday,createdate),createdate
) data
GROUP BY data.day,data.weekday, CASE data.weekday
WHEN 1 THEN 'SUN'
WHEN 2 THEN 'MON'
WHEN 3 THEN 'TUE'
WHEN 4 THEN 'WED'
WHEN 5 THEN 'THU'
WHEN 6 THEN 'FRI'
WHEN 7 THEN 'SAT'
END
order by day
This will return a table like this:
This is the ideal, but I can get queries where a day or more days are missing and then I would like to fill the missing row with the
day
,weekday
andweekdaylabel
and set for the total column the value 0.
Any clue?