2017-07-19 35 views
0

所以我想要做的是將來自3個或更多相同表格的計數添加到一個新表格中。這甚至在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的新總和?可能嗎?如果

回答

1

'Union ALL'多次打我的桌子,因爲我有多達50個ID,所以我用了一個unpivot,只是一口氣把它抓起來。

這只是一個更清潔我的需求。

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

不知道我正確地解釋你所期望的:如果你想在3個(或n)總表還是分組的名稱,你可以創建一個使用UNION ALL結果的全球數據集,然後GROUP BY名稱,總結每種顏色:

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

前三個表是你的輸入表,最後表(以紅色突出顯示)是總結價值的整體結果s跨越輸入表。

如果您需要插入更多表格,您只需將它們添加到UNION ALL部分即可。

+0

謝謝你的幫忙。 :) – SkysLastChance

相關問題