I have a table which contains records of transactions. And I have a query which retrieves those transactions out and shows the daily totals. There should always be seven rows of data displayed.
Problem. If there are transactions for each weekday in a period, the data will display correctly. However if, during the selected period, a particular weekday does not have any transactions, or a period of less than a week is selected, fewer than seven rows of daya will be displayed.
For example, I select a range of dates from, say, 2006-12-04 to 2006-12-17 (two full weeks). For whatever reason no transactions were made on 2006-12-06 or 2006-12-13 (both Wednesdays of the range) the data will display six records omitting Wednesday.
As another example, I select a range from 2006-12-04 to 2006-12-09 (Monday through Saturday). The display shows six records but omits Sunday.
How do I get the query to display all seven days, beginning with Monday and ending with Sunday, whether or not there is a transaction for each weekday?
Oh, yeah, my existing query:
Code:
SELECT
DAYNAME(TransDt) AS 'Day'
,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '07' THEN 1 ELSE 0 END) AS Hour7
,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '08' THEN 1 ELSE 0 END) AS Hour8
#DATE_FORMAT's '09' through '19' trimmed out for length
,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '20' THEN 1 ELSE 0 END) AS Hour20
,SUM(CASE WHEN DATE_FORMAT(TransDt, '%H') = '21' THEN 1 ELSE 0 END) AS Hour21
,SUM(CASE WHEN (DATE_FORMAT(TransDt, '%H') < '07') OR (DATE_FORMAT(TransDt, '%H') > '21') THEN 1 ELSE 0 END) AS HourOther
,COUNT(InvNum) AS DayTotal
,1 AS Position1
FROM
tblTransactions
WHERE
(StoreNum = 123)
AND (Date(TransDt) >= '2006-12-04')
AND (Date(TransDt) <= '2006-12-17')
GROUP BY
DAYNAME(TransDt)