我正在做一個SQL語法聯盟,我想有這樣的結果:在SQL語法聯盟合併數據
+---------------+-----+---------+
|trkBusinessUnit| New | Pending |
+---------------+-----+---------+
| AIIB 2 0 |
| Credit Control 1 3 |
| Direct Center 1 2 |
| Financial Ins 1 1 |
| Motor Acclaim 1 0 |
+-------------------------------+
從我的代碼:
SELECT trkBusinessUnit, Count(*) as New,0 as Pending
FROM tblDTPTracker
WHERE trkStatus = 'New'
GROUP BY trkBusinessUnit
UNION
SELECT trkBusinessUnit,0 as New,Count(*) as Pending
FROM tblDTPTracker
WHERE trkStatus = 'Pending'
GROUP BY trkBusinessUnit
但隨後的電流輸出是:
+---------------+-----+---------+
|trkBusinessUnit| New | Pending |
+---------------+-----+---------+
| AIIB 2 0 |
| Credit Control 1 0 |
| Credit Control 0 3 |
| Direct Center 1 0 |
| Direct Center 0 2 |
| Financial Ins 1 0 |
| Financial Ins 0 1 |
| Motor Acclaim 1 0 |
+-------------------------------+
我錯過了什麼或做錯了什麼?好心提醒。
嗨馬克感謝您的回答;然而,我有一個錯誤,說'查詢表達式'中缺少運算符sum(case'trkStatus','New'then 1 else 0 end'')。我們怎樣才能對此做出修正?謝謝 – CaptainBadass 2014-09-26 08:59:56
上面使用的case表達式不需要用於case語句的'case end'。 http://dev.mysql.com/doc/refman/5.7/en/case.html – 2014-09-26 09:17:41
嗨,馬克,我做了你說的,但仍然是一樣的。儘管它會在第一個總和句法上高亮「trkStatus」。 – CaptainBadass 2014-09-26 09:18:11