我正在使用SQL Union语法,我希望得到如下结果:
+---------------+-----+---------+
|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 |
+-------------------------------+
我错过了什么或做错了什么?请建议。
答案 0 :(得分:1)
如果我理解正确,你不必使用工会。
尝试:
SELECT
trkBusinessUnit,
COUNT(CASE WHEN trkStatus = 'New' THEN 1 ELSE NULL END) as New,
COUNT(CASE WHEN trkStatus = 'Pending' THEN 1 ELSE NULL END) as Pending
FROM tblDTPTracker
GROUP BY trkBusinessUnit
答案 1 :(得分:1)
以前的答案中存在(或已经存在)一些语法问题,但两个早期答案的意图都是正确的,您需要使用GROUP BY
查询而不使用UNION
- 这只是没有做你希望/期待的事情。
UNION或UNION ALL按行进行ROW,绝对不要通过COLUMN进行合并
因此,基于组的查询的MySQL语法可以是以下任何一种:
COUNT()使用隐式NULL
SELECT
trkBusinessUnit
, COUNT(CASE WHEN trkStatus = 'New' THEN 1 END) as New
, COUNT(CASE WHEN trkStatus = 'Pending' THEN 1 END) as Pending
FROM tblDTPTracker
GROUP BY trkBusinessUnit
;
COUNT()使用显式NULL
SELECT
trkBusinessUnit
, COUNT(CASE WHEN trkStatus = 'New' THEN 1 ELSE NULL END) as New
, COUNT(CASE WHEN trkStatus = 'Pending' THEN 1 ELSE NULL END) as Pending
FROM tblDTPTracker
GROUP BY trkBusinessUnit
;
SUM()作为计数的替代方法:
select
trkBusinessUnit
, sum(case when trkStatus = 'New' then 1 else 0 end) as New
, sum(case when trkStatus = 'Pending' then 1 else 0 end) as Pending
from tblDTPTracker
where trkStatus in ('Pending', 'New')
group by trkBusinessUnit
;
向Marc Gravell&在这个回答之前的Daniel Gadawski;这个答案是你的衍生物。
答案 2 :(得分:0)
Union垂直追加行(注意UNION
也只占用不同的行; UNION ALL
占用所有行)。您想要的是full outer join
两个子查询,或更复杂的select
。我会选择后者!
select tblDTPTracker,
sum(case trkStatus when 'New' then 1 else 0 end) as New,
sum(case trkStatus when 'Pending' then 1 else 0 end) as Pending
from tblDTPTracker
where trkStatus in ('Pending', 'New')
group by tblDTPTracker
完全外连接方法类似于:
SELECT ISNULL(x.trkBusinessUnit, y.trkBusinessUnit) as trkBusinessUnit,
ISNULL(x.New, 0) as New,
ISNULL(y.Pending, 0) as Pending
FROM (
SELECT trkBusinessUnit, Count(1) as New
FROM tblDTPTracker
WHERE trkStatus = 'New'
GROUP BY trkBusinessUnit) x
FULL OUTER JOIN (
SELECT trkBusinessUnit, Count(1) as Pending
FROM tblDTPTracker
WHERE trkStatus = 'Pending'
GROUP BY trkBusinessUnit) y on y.trkBusinessUnit = x.trkBusinessUnit