使用3个或更多表的总计数创建新表

时间:2017-07-19 18:20:09

标签: sql-server tsql sql-server-2012

所以我要做的是将3个或更多相同表中的计数添加到一个新表中。这在SQL中是否可能?

这是我的工作查询:

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的新总和?有可能吗?

2 个答案:

答案 0 :(得分: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

前三个表是您的输入表,最后一个表(以红色突出显示)是对输入表中的值求和的总结果。

如果您需要插入更多表格,则只需将其添加到UNION ALL部分。

答案 1 :(得分:1)

' Union ALL'我多次打我的桌子,因为我有50多个身份证,所以我用了一个不透明的东西,只是一次性抓住它。

这对我的需求来说有点清洁。

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