我有一個初學者問題。我的SQL表看起來像:SQL:多個選擇只有一個條件有所不同
| Date | Type | Manufacturer |
2016/04/01 A X
2016/04/01 B Y
2016/04/02 B X
2016/05/07 A Z
... ... ...
我的目標是計算兩個日期之間製造商「類型」的數量。我希望得到的結果類似以下內容:
| Manufacturer | Quantity_TypeA | Quantity_TypeB |
X 1 1
Y 0 1
Z 1 0
我的查詢是這樣的:
select Manufacturer as Manufacturer,
COUNT(*) as Quantity_TypeA
From MyTable
Where [Type] = 'A' and
Date between '20150101' and '20160930',
COUNT(*) as Quantity_TypeB
From MyTable
Where [Type] = 'B' and
Date between '20150101' and '20160930'
group by Manufacturer Order by Quantity_TypeA DESC
我也試圖在使用類型的功能,如CASE,並沒有奏效。我錯過了什麼,但是什麼?
好像你有某種日期格式不一致。列日期的數據類型? – jarlh