2012-07-24 114 views
0

我有以下查询:SQL统计汇总查询

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

任何帮助是极大的赞赏计成本的总数。

回答

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 
+0

+1,为你2秒快速:-) – 2012-07-24 14:31:22

+1

我认为触摸打字是我参加了高中最重要的类。 – 2012-07-24 14:33:18