我从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
任何人都明白为什么? 以及如何正确订购?
答案 0 :(得分:3)
user
是保留字 - 返回当前用户名
尝试
order by u.firstname || ' ' || u.surname, action
答案 1 :(得分:0)
尝试使用列的位置
进行排序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;
答案 2 :(得分:0)
尝试此订单条款: -
ORDER BY u.firstname || ''|| u.surname,action;
我认为它忽略了USER列的顺序,因为它是派生列 - 您需要指定派生本身,而不是派生的名称。