0

除了每个活动的平均“分数”和每个活动的计数之外,我有三个选择声明提供参加特定活动的成员数量 - 医疗保健、宗教和体育。

每个事件的计数会有所不同。

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

我怎么做?

 (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' )
4

3 回答 3

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 
于 2012-08-07T20:02:08.483 回答
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

等等

于 2012-08-07T20:04:15.530 回答
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'
于 2012-08-07T20:18:28.327 回答