Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

How to sort specific column when using GROUPING SETS in SQL Server?

$
0
0

If I remember correctly, in SQL Server, it is not recommended to use "ORDER BY" when there's already a GROUP BY GROUPING SETS.

I have two columns: [UPC#] & [Description] both are varchar.

My grouping sets is like this:

GROUP BY 
GROUPING SETS 
(
    ([UPC],[Description])
    ,()     
)

I don't have 'ORDER BY' but it's automatically sorting the Description column.

If I added a 3rd column, sum(Qty), then it doesn't sort by Description anymore. But if I added

ORDER BY [Description]
then the grand total of sum(Qty) will be at the first row instead of the last.

Is there a way to sort the Description column and still let the grand total of sum(Qty) be at the last row instead?

Thanks.



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>