devxlogo

GROUP BY: Sorting with an Additional Column

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;

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist