0

各イベントの平均「スコア」とカウントに加えて、特定のイベント (ヘルスケア、宗教、スポーツ) に参加するメンバーの数を提供する 3 つの select ステートメントがあります。

回数はイベントごとに異なります。

各クエリは個別に機能しますが、それらを 1 つのクエリに結合したいと考えています。

それ、どうやったら出来るの?

 (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

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