我有两个临时表(下面),第一个表示五个条件之一。第二个从该表中拉出并根据条件进行求和和计数。我怎么能让第二个表格使用这个或类似的格式?
select ID
,sum_value
,condition_field
,'condition_1' = case when condition_type in (1,2) then 1 else 0 end
,'condition_2' = case when condition_type in (3,4) then 1 else 0 end
--etc...
into #temp
from my_table
select ID
,sum_value
,'1_amt' = SUM(sum_value) from #temp where condition_1 = 1
,'1_cnt' = COUNT(ID) from #temp where condition_1 = 1
,'2_amt' = SUM(sum_value) from #temp where condition_2 = 1
,'2_cnt' = COUNT(ID) form #temp where condition_2 = 2
from #temp
答案 0 :(得分:1)
你想要更像这样的东西:
SUM(CASE WHEN condition_1=1 THEN sum_value ELSE 0 END) AS 1_amt