【问题标题】:Can someone help me in creating a dummy query for following use case有人可以帮我为以下用例创建一个虚拟查询吗
【发布时间】:2022-10-12 23:09:01
【问题描述】:

在下图中,一个是问题陈述,另一侧是我需要的结果。有人可以帮我处理 sql 查询吗?

【问题讨论】:

    标签: sql postgresql pivot


    【解决方案1】:

    从您的示例中,我将使用以下内容:

    
    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看到我的例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2017-08-01
      • 2021-04-25
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多