Skip to content
Advertisement

SQL creating overview for data that does not exist

I need a script that will give me a total sum of amounts per sort for the past 3 days and upcoming week. I have made a script (link to dbfiddle.uk) that will sum the amount of the grouped date and sort types. However I am unclear on how to move forward at this point.

I want the past 3 days and upcoming 7 days of the today’s date (dynamically), not just the dates found in my table. I also want to show all the sort types.

So if there are no records for the date and sort type, show 0 as result.

plannings table

planning_amounts table

sort_types table

The expected result would look like this. (this obviously for the past 3 + upcomming 7 days)

Query

Advertisement

Answer

Hope I understood your question a bit better now:

Here’s the fiddle: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c508639ba4ec5bd49b49c9afe0692c9f

If I missunderstood yet again, please clarify where 🙂

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement