In sql I want to show statistics data, so I’d like to show sums from all weekdays, by showing 0 for the weekday that doesn’t exist. Same goes with month, hours, years….
The problem is that there is no table to join to to get the missing days.
Is there a clean method to do this?
Thank you
Edit :
The DB and data is irrelevent but I’m using Oracle DB and Mysql, simply put if i want to do
SELECT SUM(SALES) FROM SALES_TABLE WHERE SALES_DATE BETWEEN DATE1 AND DATE2 GROUP BY WEEKDAY
how to change this to get 0 for example on sundays, and not get SATURDAY :4, MONDAY:4
You could create a table with day names in it, and then do a left join on that table.
It’d help to have a look at the table structure you have, but if I was going to do it, I’d do something like the following:
You’ll have nulls for the sums on dates that aren’t represented in your data, but you can convert them to zeros, depending on your database server.