Skip to content
Advertisement

SQL Server : how to group only part of the syntax

I have a problem creating a SQL Server query.

In summary, the query should get columns that are sum and count, grouped by customerID, and another column that is a case when by a column that is not used as a grouper column.

My problem is to group only part of the syntax, while the case when column does not need to be grouped.

A sample data, Test:

customerID InvoiceID ProductID Date Income
1 1234551 A 01/01/2015 300
2 1234552 B 02/01/2016 300

I have a solution, but I am sure there is a more simple solution.

To produce:

customerID Income_2015 Income_2016 Invoices Purchase_product_A
1 300 300 2 TRUE
10 0 400 1 FALSE

Thanks! Nir

Advertisement

Answer

You may use conditional aggregation with a single pass query:

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