Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

How to sort numbers that are in varchar type under GROUP BY GROUPING SETS in SQL Server?

$
0
0

I have a column called UPC (varchar type) that only contain numbers. I also have a GROUP BY GROUPING SETS on UPC.

Currently, it's sorted as varchar, which result the numbers not sorted from smallest to largest.

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

enter image description here

If I use convert the UPC from varchar to bigint, the numbers will be sorted alphabetically but then my last row generated from GROUPING SETS will move from last to first.

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

ORDER BY convert(bigint, UPC)

enter image description here

Is there a way to make my "grand total" move to the last row and still have my numbers sorted alphabetically? I'm guessing i might need to use GROUPING_ID?



Viewing all articles
Browse latest Browse all 8428

Trending Articles