2017-07-19 33 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

相关问题