Skip to content
Advertisement

Multiple SUM in one query with multiple where

I’m trying to write a SQL query that produces a table with summarized values for each year and month.

I have a table that looks something like this:

And another table that looks like this:

I want to have a query that returns the sum for each month like this:

Explanation of the output:

Year and Month are obvious

RegTime should be the sum of all TIME.cTime WHERE CASE.invoice <> ‘NoFakt’

Invoiced should be the sum of all TIME.cTIME WHERE CASE.invoice = ‘Yes’ AND CASE.status = ‘finish’

ToBeInvoiced should be the sum of all TIME.cTIME WHERE CASE.invoice = ‘Yes’ AND CASE.status <> ‘finish’

I have tried this query, but with this i need to loop a predefined year and month value in my programming code instead of simply having a sql statement that are doing all the work. There´s got to be a simpler way than this…

Advertisement

Answer

Use conditional aggregation :

Note: Do not use reserved keyword as Object Name.

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