Hello想知道如何使用PostgreSQL to sum()“CALL ID count”,然后将结果放在列表的底部。我试过联合和分组。

AGENT   CALL ID count
xxxxx   13
xxxxx   21
xxxxx   49
xxxxx   58
xxxxx   67
xxxxx   32
xxxxx   9
xxxxx   8
xxxxx   39
xxxxx   1047


AGENT   CALL ID count
xxxxx   13
xxxxx   21
xxxxx   49
xxxxx   58
xxxxx   67
xxxxx   32
xxxxx   9
xxxxx   8
xxxxx   39
xxxxx   1047
TOTAL   1343

Select Agent,Sum("CALL ID count") as TOTAL
from AgentCount
group by "CALL ID count"
order by Agent

我希望能把探员的名字记下来,然后在最后加上总数
谢谢你

最佳答案

您必须使用联合来合并这两个查询,然后相应地使用ORDER BY,请看下面的示例:

Select Agent,Sum("CALL ID count") as TOTAL
from AgentCount
group by Agent
UNION
Select 'TOTAL' AS Agent ,Sum("CALL ID count") as TOTAL FROM AgentCount
ORDER BY CASE WHEN Agent = 'TOTAL' THEN 1 ELSE 0 END ASC, Agent

关于sql - 总结数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28243945/

10-13 05:27