2017-07-19 7 views
0

これは、3つ以上の同一テーブルからのカウントを1つの新しいテーブルに追加することです。これはSQLでも可能ですか?3つ以上のテーブルからの合計カウントで新しいテーブルを作成する

これは私が持っている作業クエリです:

select FirstID, 
    sum(case when Color = 'Red' then 1 else 0 end) 'RED', 
    sum(case when Color = 'Blue' then 1 else 0 end) 'BLUE', 
    sum(case when Color = 'Green' then 1 else 0 end) 'GREEN', 
    sum(case when Color = 'Yellow' then 1 else 0 end) 'YELLOW' 
from Table 
group by FirstID 
order by PrimaryDiagnosisCode 

select SecondID, 
    sum(case when Color = 'Red' then 1 else 0 end) 'RED', 
    sum(case when Color = 'Blue' then 1 else 0 end) 'BLUE', 
    sum(case when Color = 'Green' then 1 else 0 end) 'GREEN', 
    sum(case when Color = 'Yellow' then 1 else 0 end) 'YELLOW' 
from Table 
group by SecondID 
order by SecondID 

select ThirdID, 
    sum(case when Color = 'Red' then 1 else 0 end) 'RED', 
    sum(case when Color = 'Blue' then 1 else 0 end) 'BLUE', 
    sum(case when Color = 'Green' then 1 else 0 end) 'GREEN', 
    sum(case when Color = 'Yellow' then 1 else 0 end) 'YELLOW' 
from Table 
group by ThirdID 
order by ThirdID 

だから私は、私はこのように見える3つのテーブル持っているクエリを実行した後:私は追加するクエリを記述しますどのように

Name  RED  BLUE GREEN  YELLOW 
-----  ----- ------ ------- ---------- 
ColorID1 52  1  3   5 
ColorID2  2  27  73   9 
ColorID3  0  2  3   50 

を3つのテーブルのすべてのIDの新しい合計を持つテーブル?出来ますか?あなたはUNION ALLを使用して結果のグローバルデータセットを作成し、GROUP BY名を、それぞれの色を合計することができます名前で、まだグループ化されたテーブルを3(またはn)の間で合計をしたい場合は:私が正しくあなたが期待するものと解釈

答えて

1

私は50までのIDを持っているので、ユニオンALLが何度も私のテーブルに当たっていたので、私はアンピボットを代わりに使いました。

これは私のニーズにちょっとしたものです。

select * 
into #Init1 
from Table 
where Color in ('Red','Blue','Green','Yellow') 
     and ServiceDate >= '2016-01-01' 
     and ServiceDate <= '2016-12-13' 

select distinct Color, AccountID, TransactionID 
,Diag 
into #Diags 
from #Init1 
unpivot 
(
     Diag 
     for Problem in (FirstID,SecondID,ThirdID) 
) as unpvt 
order by Color, AccountID,TransactionID 



select Diag, 
    sum(case when Color = 'Red' then 1 else 0 end) 'RED', 
    sum(case when Color = 'Blue' then 1 else 0 end) 'BLUE', 
    sum(case when Color = 'Green' then 1 else 0 end) 'GREEN', 
    sum(case when Color = 'Yellow' then 1 else 0 end) 'YELLOW' 
from #Diags 
group by Diag 
1

わからない場合

ここ
declare @table_1 table([Name] nvarchar(50), RED int, BLUE int, GREEN int, YELLOW int) 
declare @table_2 table([Name] nvarchar(50), RED int, BLUE int, GREEN int, YELLOW int) 
declare @table_3 table([Name] nvarchar(50), RED int, BLUE int, GREEN int, YELLOW int) 

insert into @table_1 values 
('ColorID1',52,1,3,5),('ColorID2',2,27,73,9),('ColorID3',0,2,3,50) 

insert into @table_2 values 
('ColorID1',1,2,3,4),('ColorID2',5,6,7,8),('ColorID3',9,10,11,12) 

insert into @table_3 values 
('ColorID1',10,20,30,40),('ColorID2',50,60,70,80),('ColorID3',90,100,110,120) 

select * from @table_1 
select * from @table_2 
select * from @table_3 

select tmp.[Name], SUM(tmp.RED) as RED, SUM(tmp.BLUE) as BLUE, SUM(tmp.GREEN) as GREEN, 
    SUM(tmp.YELLOW) as YELLOW 
from (
    select [Name],RED,BLUE,GREEN,YELLOW from @table_1 union all 
    select [Name],RED,BLUE,GREEN,YELLOW from @table_2 union all 
    select [Name],RED,BLUE,GREEN,YELLOW from @table_3 
    --you can add more tables here 
) tmp 
group by tmp.[Name] 

結果である:

enter image description here

が最初の3つのテーブルがあなたの入力テーブルである、(赤で強調)の最後のテーブルには、値を合計し、全体的な結果でありますsを入力テーブルに渡します。

さらにテーブルを挿入する必要がある場合は、UNION ALLセクションにテーブルを追加するだけです。

+0

ありがとうございました。 :) – SkysLastChance

関連する問題