有没有人可以帮我为下面的用例创建一个虚拟查询

ykejflvf  于 2022-10-15  发布在  PostgreSQL
关注(0)|答案(2)|浏览(170)

在下面的图片中,一个是问题陈述,另一边是我需要的结果。有没有人能帮我做一下SQL查询。

x7yiwoj4

x7yiwoj41#

对不起,我已经完成了MS SQL的第一个答案。对Postgres的第二个答复:

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)
;

并且输出显示为

您可以在http://sqlfiddle.com/#!17/12dc77/2上看到我的示例

mklgxw1f

mklgxw1f2#

SQL Server示例-Postgres的第二个响应

从您的样例中,我将使用类似以下内容:

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

我使用以下命令创建了测试:

CREATE TABLE Dummy
    ([ID] int, [ClosedDate] int, [RepliedDate] int)
;

INSERT INTO Dummy
    ([ID], [ClosedDate], [RepliedDate])
VALUES
    (1, 10, 11),
    (2,12,11),
    (3,10,12),
    (4,11,12)
;

并且输出显示为

您可以在http://sqlfiddle.com/#!18/c8ae79/3上看到我的示例

相关问题