GROUP BY: Sorting with an Additional Column

Question:
How do I arrange to sort within a GROUP BY clauserather 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 sortedas 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;

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: