Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Mar 24, 1997

GROUP BY: Sorting with an Additional Column

Question:
How do I arrange to sort within a GROUP BY clause rather than over the complete result set, e.g.
    L0    PDV  ......
    L90   PDV  ......
    L180  PDV  ......
    L270  PDV  ......

    L0    PPE  ......
    L90   PPE  ......
I want to force col1 to be sorted within each group.

Answer:
If you GROUP BY both columns, the results will be sorted as you've described:

    SELECT col1, col2 FROM Table
        [WHERE clause if necessary]
    GROUP BY col2, col1;
Alternatively, you could add the ORDER BY clause:
    SELECT col1, col2 FROM Table
        [WHERE clause if necessary]
    GROUP BY col1, col2
    ORDER BY col2;
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date