Skip to content
Advertisement

How to group by row in diferents columns

I have this table in SQL, I need to group it by the report name and I need to split the price_bucket column and the count_sales column in diferents rows.

I think that I should use the PIVOT funtion in SQL Server and an intermediate table. How should I do it?

This is what the final table should look like:

More info: The order_number column sometimes will have 5 rows and sometimes will have 4. In that case the last 2 columns must have 0. But never more than 5 rows.

Thank you so much for the help.

Advertisement

Answer

If I understand correctly, you need to list the buckets in order by the sales descending. You can use row_number() and conditional aggregation:

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