Skip to content
Advertisement

How to use order by after group by sql

I have a table like this:

start table

and I need to group by and order on positionNr.

I tried with this query:

SELECT campus,
    building,
    department,
    officeNr,
    officeName,
    positionNr,
FROM organizationStruct
group by campus, building, department, officeNr, officeName, positionNr
order by positionNr

But I obtain something like this:

result table

But what I want is a result like this:

expected result

Please can you help to understand where is the problem on the query?

Advertisement

Answer

Based on your expected result, it seems that: 1- You don’t need a group by, we use group by for aggregate functions. 2- You are ordering by campus first, positionNr after

Try this query

SELECT campus,
building,
department,
officeNr,
officeName,
positionNr,
FROM organizationStruct
order by campus, positionNr
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement