我想在底部添加一個總計,似乎並不理解如何獲取語法以向我顯示底部的「總計」。我用盡了在線搜索的時間。我需要合併片段,因爲我需要爲沒有銷售的員工顯示零。我看了一下這個鏈接,但我需要合併中的零。在子查詢中添加總計
Add a row for TOTAL in a sql query result
select t2.Employee, coalesce(t1."This Week",0) "This Week"
from mytable t2 left outer join
(select case when grouping(Employee) = 1 then 'Total' else Employee end, sum(Sales) "This Week"
from information
where Week >= DATE '01/01/2017' and Week < DATE '01/31/2017'
and LastUpdate >= DATE '01/01/2017' and LastUpdate < DATE '01/31/2017'
group by Employee with Rollup) t1
on t1.Employee = t2.Employee
結果:
Employee This Week
Batman 15
Penguin 25
Joker 0
Bane 5
Scarecrow 0
-------------------> 45
錯誤:
ERROR: syntax error at or near "with"
LINE 8: group by Employee with Rollup) t1
更改你的'集團by'聲明'集團通過員工提供Rollup'看如果可行 –
您正在尋找['ROLLUP'](https://www.postgresql.org/docs/devel/static/queries-table-expressions.html)。 –
在With顯示錯誤... –