I have the following code. The 3 decimal places are being displayed but the average is wrong. Is there a way I can restructure the code to achieve the correct result?
with total_indi_days as
(
SELECT COUNT(*) AS total, DATENAME(DW, DateLog) AS NameOfDay
FROM Access
GROUP BY DATENAME(DW, DateLog)
--ORDER BY total DESC
)
,total_overall_days as
(
SELECT COUNT(*) as total_days FROM Access
)
select str(((total * 100)/ total_days ), 7, 3) as average, total, total_days, NameOfDay
from total_indi_days, total_overall_days
result…
|average | total | total_days | NameOfDay
| 2.000 | 29 | 1000 | Sun
| 18.000 | 188 | 1000 | Mon
| 15.000 | 159 | 1000 | Tues
| 20.000 | 207 | 1000 | Wed
| 19.000 | 194 | 1000 | Thur
| 17.000 | 171 | 1000 | Fri
| 5.000 | 52 | 1000 | Sat
should be…
|average | total | total_days | NameOfDay
| 2.900 | 29 | 1000 | Sun
| 18.800 | 188 | 1000 | Mon
| 15.900 | 159 | 1000 | Tues
| 20.700 | 207 | 1000 | Wed
| 19.400 | 194 | 1000 | Thur
| 17.100 | 171 | 1000 | Fri
| 5.200 | 52 | 1000 | Sat
I would also like to know if there is a easier way to get this result. The “with” is a bit much, considering I have the AVG built in function, which did not work for me maybe because I was doing the wrong thing.
Change
to
This way, you don’t have integer division…