0

我正在从 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

有谁明白为什么?以及如何正确订购?

4

3 回答 3

3

user是保留字 - 返回当前用户名

尝试

order by u.firstname || ' ' || u.surname, action
于 2012-09-13T12:18:27.913 回答
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;
于 2012-09-13T12:19:17.310 回答
0

试试这个顺序子句:-

按你的名字订购 || ' ' || u.姓氏,行动;

我认为它忽略了列 USER 的顺序,因为这是一个派生列 - 您需要指定派生本身,而不是派生的名称。

于 2012-09-13T12:22:54.883 回答