Select tbl.Month, MAX(tbl.ClosedCount) as closedCount, MAX(tbl.RepliedCount) as RepliedCount
From
((select closedDate as Month, COUNT(*) ClosedCount,0 as RepliedCount
from Dummy
group by ClosedDate)
UNION
(select repliedDate as Month, 0 as ClosedCount, COUNT(*) RepliedCount
from Dummy
group by repliedDate)) as tbl
group by Month
order by Month
我使用以下命令创建了测试:
CREATE TABLE dummy(
ID int PRIMARY KEY,
ClosedDate INT,
RepliedDate INT
);
INSERT INTO Dummy
(ID, ClosedDate, RepliedDate)
VALUES
(1, 10, 11),
(2,12,11),
(3,10,12),
(4,11,12)
;
Select tbl.Month, MAX(tbl.ClosedCount) as closedCount, MAX(tbl.RepliedCount) as RepliedCount
From
((select closedDate as Month, COUNT(*) ClosedCount,'' as RepliedCount
from Dummy
group by ClosedDate)
UNION
(select repliedDate as Month, '' as ClosedCount, COUNT(*) RepliedCount
from Dummy
group by repliedDate)) as tbl
group by Month
2条答案
按热度按时间x7yiwoj41#
对不起,我已经完成了MS SQL的第一个答案。对Postgres的第二个答复:
我使用以下命令创建了测试:
并且输出显示为
您可以在http://sqlfiddle.com/#!17/12dc77/2上看到我的示例
mklgxw1f2#
SQL Server示例-Postgres的第二个响应
从您的样例中,我将使用类似以下内容:
我使用以下命令创建了测试:
并且输出显示为
您可以在http://sqlfiddle.com/#!18/c8ae79/3上看到我的示例