所以我想要做的是将来自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的新总和?可能吗?如果
谢谢你的帮忙。 :) – SkysLastChance