これは、3つ以上の同一テーブルからのカウントを1つの新しいテーブルに追加することです。これは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の新しい合計を持つテーブル?出来ますか?あなたはUNION ALL
を使用して結果のグローバルデータセットを作成し、GROUP BY
名を、それぞれの色を合計することができます名前で、まだグループ化されたテーブルを3(またはn)の間で合計をしたい場合は:私が正しくあなたが期待するものと解釈
ありがとうございました。 :) – SkysLastChance