Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Application Security Testing: An Integral Part of DevOps

GROUP BY: Sorting with an Additional Column

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.

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.



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