所以我想要做的是將來自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