you need to count the number of days per weekday separatly and join this to your result set.
Something like:
select d.AREA_NM_TURF,
d.DayofWeek,
casT(d.CountbyDayofWeek as decimal(15,3)) / w.num_of_days as AVG_COUNT
from
(
select
CASE day_of _week
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 DayofWeek,
count(*) as num_of_days
from sys_calendar.calendar
where calendar_date Between '2012-08-01' AND '2012-08-30'
group by 1
) w
join
(
SELECT
AREA_NM_TURF,
Count (*) as CountbyDayofWeek,
CASE ((CMTMT_DT - Date '1900-01-01') Mod 7) + 1
WHEN 7 THEN 'Sun'
WHEN 1 THEN 'Mon'
WHEN 2 THEN 'Tue'
WHEN 3 THEN 'Wed'
WHEN 4 THEN 'Thu'
WHEN 5 THEN 'Fri'
WHEN 6 THEN 'Sat' END as DayofWeek
FROM VCTD485
Where (ASKME_CREAT_DT) Between '2012-08-01' AND '2012-08-30'
AND CMTMT_DT = ASKME_CREAT_DT
And Area_NM_TURF like 'AL%PCA'
Group by AREA_NM_Turf, DAyofWeek
) as d
on d.DayofWeek = w.DayofWeek
order by d.AREA_NM_TURF,
d.DayofWeek
↧