0

I am selecting a name, an action and a count from Postgres sorting by name ASC, action ASC like so:

SELECT u.firstname || ' ' || u.surname AS user,
    nt.name AS action,
    cn.count
FROM (
    SELECT actioned_by_id, note_type_id, COUNT(id) AS count
    FROM customer_notes
    WHERE actioned_date IS NOT NULL 
    GROUP BY actioned_by_id, note_type_id
) AS cn
LEFT JOIN note_type AS nt ON cn.note_type_id = nt.id
LEFT JOIN users as u on cn.actioned_by_id = u.id
WHERE cn.actioned_by_id IS NOT NULL 
ORDER BY user, action;

however the results show that it is ignoring the user clause and only ordering by the action.

"ADMIN USER" "CALL OUT"   1
"ADMIN USER" "EMAIL"      1
"ADMIN USER" "LETTER"     2
"AA AA"      "MEETING"    1
"ADMIN USER" "PHONECALL"  7
"AA AA"      "PHONECALL"  1

Anyone understand why? And how to make it order properly?

3 Answers 3

3

user is a reserved word - returning the current user name

Try

order by u.firstname || ' ' || u.surname, action
Sign up to request clarification or add additional context in comments.

6 Comments

That was it. Thanks! I changed it to AS name instead and sorted on that.
@mouckatron You don't need the concatenation between firstname and surname when doing ORDER BY. This would suffice: ORDER BY u.firstname,u.surname. I cringe when I see concatenation on ORDER BY, it's useless, and has side-effects, it's for you to find out
@MichaelBuen You can't possibly say that without knowing the values of the data. If there are two records ('a b', 'c') and ('a', 'b c') your proposal returns a different result. In order to preserve the intent of the original query, the concatenation is necessary.
So you've given a SQL fiddle where the resultset proves me right, and still you can't see the difference?
@podiluska RE: "proves you right"? You can't even see that ordering by concatenation would return wrong result? Zuckerberg should come last. Your query return Zuckerberg as second. There's nothing sqlfiddle(concatenation approach vs ordering by fields directly) that proves you right, in fact it indicates otherwise. Your query is really not correct
|
0

Try this order clause:-

ORDER BY u.firstname || ' ' || u.surname, action;

I think it is ignoring the order by column USER, as that is a derived column - you need to specify the derivation itself, not the name of the derivation.

Comments

0

Try to order using columns' positions

SELECT u.firstname || ' ' || u.surname AS user,
    nt.name AS action,
    cn.count
FROM (
    SELECT actioned_by_id, note_type_id, COUNT(id) AS count
    FROM customer_notes
    WHERE actioned_date IS NOT NULL 
    GROUP BY actioned_by_id, note_type_id
) AS cn
LEFT JOIN note_type AS nt ON cn.note_type_id = nt.id
LEFT JOIN users as u on cn.actioned_by_id = u.id
WHERE cn.actioned_by_id IS NOT NULL 
ORDER BY 1, 2;

1 Comment

Good answer which would also work now that I've read the documentation on order by more thoroughly. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.