我正在从 Postgres 按名称 ASC 排序中选择一个名称、一个操作和一个计数,操作 ASC 如下所示:
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;
但是结果表明它忽略了用户子句,仅按操作排序。
"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
有谁明白为什么?以及如何正确订购?