带条件的SQL双聚合函数

带条件的SQL双聚合函数,sql,postgresql,group-by,aggregate-functions,window-functions,Sql,Postgresql,Group By,Aggregate Functions,Window Functions,我有一张桌子看起来像是空的 DepartmentId | Salary | Status ---------------------------------- 1 | 5 | active 1 | 10 | active 1 | 15 | passive 1 | 20 | passive 我要选择此具有衰减输出的表 Col

我有一张桌子看起来像是空的

DepartmentId    | Salary  | Status       
----------------------------------
1               |  5     | active
1               |  10     | active
1               |  15     | passive
1               |  20     | passive
我要选择此具有衰减输出的表

Columns:
DepartmentId,
Sum of salary where status is active ,
Sum of salary where status is passive.


DepartmentId    | Sum(salary) where status is active  | Sum(salary) where status is passive       

-------------------------------------------------------------------------------------------
    1           |  15                                 | 35
我使用postgresql作为数据库。 如何查询该表以获得作为上限的输出?
谢谢。

这很简单,您是想在这方面进行优化,还是想让我们为您编写查询

Select DepartmentId,
Sum (case when status='active' then salary end) as SumOfActive ,
Sum (case when status='passive' then salary end) as SumOfPassive
From TableName
Group By DepartmentId

这很简单,您是想在这方面进行优化,还是想让我们为您编写查询

Select DepartmentId,
Sum (case when status='active' then salary end) as SumOfActive ,
Sum (case when status='passive' then salary end) as SumOfPassive
From TableName
Group By DepartmentId

“或者你想让我们为你写查询?”哈!我敢肯定这是“为我写密码!”问题好的第一个(或第三个)答案…欢迎来到SO并保持它的不安,伙计,起初我选择了错误的标题来回答我的问题,我认为这个问题对我来说已经足够优化了。谢谢你的回答。“或者你想让我们为你写查询?”哈!我敢肯定这是“为我写密码!”问题好的第一个(或第三个)答案…欢迎来到SO并保持它的不安,伙计,起初我选择了错误的标题来回答我的问题,我认为这个问题对我来说已经足够优化了。谢谢你的回答。