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 に答える