组合选择语句

时间:2012-08-07 19:57:24

标签: sql select

我有三个选择陈述,提供参加特定活动的会员数量 - 医疗保健,宗教和体育 - 除了每个活动的平均“得分”和每个活动的计数。

每个事件的计数都不同。

每个查询单独工作,但我想将它们合并为一个查询。

我该怎么做?

 (select sum(case when Healthcare ='1' then 1 else 0 end)  as [Healthcare_never],
 sum(case when Healthcare ='2' then 1 else 0 end)  as [Healthcare_not often],
 sum(case when Healthcare ='3' then 1 else 0 end)  as [Healthcare_average],
 sum(case when Healthcare ='4' then 1 else 0 end)  as [Healthcare_often],
 sum(case when Healthcare ='5' then 1 else 0 end)  as [Healthcare_very often]
 ,avg(Cast(Healthcare as float)) as Average
 ,count(Healthcare) as N_Healthcare
 from Member 
 where Healthcare > '0' )


 (select
  sum(case when Religious ='1' then 1 else 0 end)  as [Religious_never],
  sum(case when Religious ='2' then 1 else 0 end)  as [Religious_not often],
  sum(case when Religious ='3' then 1 else 0 end)  as [Religious_average],
  sum(case when Religious ='4' then 1 else 0 end)  as [Religious_often],
  sum(case when Religious ='5' then 1 else 0 end)  as [Religious_very often],
  Avg(cast(Religious as float)) as Average
  ,count(Religious) as N_Religious
  from Member 
  where Religious > '0' )


 (select
 sum(case when Sport ='1' then 1 else 0 end)  as [Sport_never],
 sum(case when Sport ='2' then 1 else 0 end)  as [Sport_not often],
 sum(case when Sport ='3' then 1 else 0 end)  as [Sport_average],
 sum(case when Sport ='4' then 1 else 0 end)  as [Sport_often],
 sum(case when Sport ='5' then 1 else 0 end)  as [Sport_very often],
 Avg(cast(Sport as float)) as Average
 ,count(Sport) as N_Sport
 from Member 
 where Sport > '0' )

3 个答案:

答案 0 :(得分:3)

试试这个

select 
sum(case when Healthcare ='1' then 1 else 0 end)  as [Healthcare_never],
sum(case when Healthcare ='2' then 1 else 0 end)  as [Healthcare_not often],
sum(case when Healthcare ='3' then 1 else 0 end)  as [Healthcare_average],
sum(case when Healthcare ='4' then 1 else 0 end)  as [Healthcare_often],
sum(case when Healthcare ='5' then 1 else 0 end)  as [Healthcare_very often],
avg(Cast((case when Healthcare > 0 Then Healthcare Else Null end) as float)) as Healthcare_Average,
count(case when Healthcare > 0 Then Healthcare Else Null end) as N_Healthcare,

sum(case when Religious ='1' then 1 else 0 end)  as [Religious_never],
sum(case when Religious ='2' then 1 else 0 end)  as [Religious_not often],
sum(case when Religious ='3' then 1 else 0 end)  as [Religious_average],
sum(case when Religious ='4' then 1 else 0 end)  as [Religious_often],
sum(case when Religious ='5' then 1 else 0 end)  as [Religious_very often],
Avg(cast((case when Religious > 0 Then Religious Else Null end) as float)) as Religious_Average,
count(case when Religious > 0 Then Religious Else Null end) as N_Religious,

sum(case when Sport ='1' then 1 else 0 end)  as [Sport_never],
sum(case when Sport ='2' then 1 else 0 end)  as [Sport_not often],
sum(case when Sport ='3' then 1 else 0 end)  as [Sport_average],
sum(case when Sport ='4' then 1 else 0 end)  as [Sport_often],
sum(case when Sport ='5' then 1 else 0 end)  as [Sport_very often],
Avg(cast((case when Sport > 0 Then Sport Else Null end) as float)) as Sport_Average,
count(case when Sport > 0 Then Sport Else Null end) as N_Sport

from Member 

答案 1 :(得分:2)

将UNION ALL放在SELECT语句之间。但是你可能想要添加另一列来指示它来自哪个表。列标题必须相同。例如:

 select 'Healthcare' as source
, sum(case when Healthcare ='1' then 1 else 0 end)  as [never], 
 sum(case when Healthcare ='2' then 1 else 0 end)  as [not often], 
 sum(case when Healthcare ='3' then 1 else 0 end)  as [average], 
 sum(case when Healthcare ='4' then 1 else 0 end)  as [often], 
 sum(case when Healthcare ='5' then 1 else 0 end)  as [very often] 
 ,avg(Cast(Healthcare as float)) as Average 
 ,count(Healthcare) as N_Healthcare 
 from Member  
 where Healthcare > '0' 
UNION ALL

等。

答案 2 :(得分:1)

问题是where语句为每个事件提供了错误的计数。 医疗保健计数应为54,运动54和其他13 相反,所有人的数量都是55.

 select 
 sum(case when Healthcare ='1' then 1 else 0 end)  as [Healthcare_never],
 sum(case when Healthcare ='2' then 1 else 0 end)  as [Healthcare_not often],
 sum(case when Healthcare ='3' then 1 else 0 end)  as [Healthcare_average],
 sum(case when Healthcare ='4' then 1 else 0 end)  as [Healthcare_often],
 sum(case when Healthcare ='5' then 1 else 0 end)  as [Healthcare_very often],
 avg(Cast(Healthcare as float)) as Average,
 count(Healthcare) as N_Healthcare,

 sum(case when Religious ='1' then 1 else 0 end)  as [Religious_never],
 sum(case when Religious ='2' then 1 else 0 end)  as [Religious_not often],
 sum(case when Religious ='3' then 1 else 0 end)  as [Religious_average],
 sum(case when Religious ='4' then 1 else 0 end)  as [Religious_often],
 sum(case when Religious ='5' then 1 else 0 end)  as [Religious_very often],
 Avg(cast(Religious as float)) as Average,
 count(Religious) as N_Religious,

 sum(case when Sport ='1' then 1 else 0 end)  as [Sport_never],
 sum(case when Sport ='2' then 1 else 0 end)  as [Sport_not often],
 sum(case when Sport ='3' then 1 else 0 end)  as [Sport_average],
 sum(case when Sport ='4' then 1 else 0 end)  as [Sport_often],
 sum(case when Sport ='5' then 1 else 0 end)  as [Sport_very often],
 Avg(cast(Sport as float)) as Average,
 count(Sport) as N_Sport
 from contacts_cstm 
 where Sport > '0' or Religious > '0' or Healthcare >'0'