Skip to content
Advertisement

CASE condition and SUM() common table expressions or Group By

Hope some can help ? I have a 3rd party software were i can do custom sql querys but not able to change any of the database. Struggling to get the result i would like.

Do I use GROUP BY or CTE to get the result i need?

This give me the result below . I have removed Where c=1 in the last select which i will put back in the final query which give me the first result by time which is what i want. The bit i am struggling with is. I would like to add the prices of all products together and the prices of all service together to give a total of products and services sold. Then a grand total of services and product together in the final query .

The Result i would like is below. The final part of the query is as below. I am only able to custom query’s in the 3rd party software.

I hope I have formatted this right for people to understand. Thank you for any help you can provide.

Advertisement

Answer

Seems like you simply need to add two Group Sums to your existing CTE and then add them in your final Select:

Btw, the ORDER BY tblTicketsRow.fldTicketID on your ROW_NUMBER is not based on a unique column, thus the order of rows is not guaranteed. You probably want order by tblTicketsRow.fldStartTime instead.

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