我有一個MEMBERS
表具有以下相關欄目:如何從多個SQL選擇器加入彙總結果?
Name
JoinDate
Level --1=Gold,2=Silver,3=Bronze**
我想創建一個查詢返回一個成員總結,列出誰參加了由年份和會員級別的總數。基本上,我的結果集的列會是這樣的:
| YEAR | GOLD | SILVER | BRONZE | TOTAL |
我能,銀,銅獎成員分別採用以下3個查詢得到每年不同的計數黃金:
SELECT YEAR(JoinDate) AS YEAR, COUNT(*) AS GOLD FROM Members
WHERE Level=1 GROUP BY YEAR(JoinDate) ORDER BY YEAR(JoinDate)
SELECT YEAR(JoinDate) AS YEAR, COUNT(*) AS SILVER FROM Members
WHERE Level=2 GROUP BY YEAR(JoinDate) ORDER BY YEAR(JoinDate)
SELECT YEAR(JoinDate) AS YEAR, COUNT(*) AS BRONZE FROM Members
WHERE Level=3 GROUP BY YEAR(JoinDate) ORDER BY YEAR(JoinDate)
我也可以用類似的查詢獲得總計:
SELECT YEAR(JoinDate) AS YEAR, COUNT(*) AS TOTAL FROM Members
GROUP BY YEAR(JoinDate) ORDER BY YEAR(JoinDate)
我的問題是我還沒有找到一種方法來簡化所有這些到一個查詢。這是如何完成的?