0

我有以下查询:

select prop_id
    , sum(amount)bnp_spent
    , (select count(*) from cost where cost_type = 'Direct Cost')direct
    , (select count(*) from cost where cost_type = 'Burden Cost')burden
from cost                                     
group by prop_id

子查询不是我想要的。通过从成本表中选择,我得到所有道具的直接或负担成本总数

我想要的是每个 prop_id 的直接成本和负担成本的计数

任何帮助是极大的赞赏。

4

1 回答 1

2

试试这个:

select prop_id, sum(amount) as bnp_spent,
       sum(case when cost_type = 'Direct Cost' then 1 else 0 end) as direct,
       sum(case when cost_type = 'Burden Cost' then 1 else 0 end) as burden
from cost
group by prop_id
于 2012-07-24T14:30:12.673 回答