如何按月和年分组并分隔条目?

How to group by month and year and also separate the entries?

我有一个 table [MY_TABLE],其中包含以下数据:日期 [DOCUMENT_DATE] 和状态 [STATUS]。我想分开并计算 3 个不同的状态:状态 < 8 时打开,状态 = 8 时丢失或状态 > 8 时获胜,同时按月和年分组。

最终结果将是这样的:year, month, count(won), count(lost), count(open),有效地给出每个月每个状态的计数。

有些月份根本没有状态(可以忽略)有些只有一些状态而不是全部(应该正确填写月份和年份)

我现在有一个有效的查询,但它确实很大 :

SELECT
CASE WHEN "open".year IS NOT NULL
    THEN
      "open".year
  ELSE
    (CASE WHEN "lost".year IS NOT NULL
      THEN
        "lost".year
     ELSE
       "won".year
     END)
  END          AS "Année",
  CASE WHEN "open".month IS NOT NULL
    THEN
      "open".month
  ELSE
    (CASE WHEN "lost".month IS NOT NULL
      THEN
        "lost".month
     ELSE
       "won".month
     END)
  END          AS "Mois",
  "open".count AS "Ouvertes",
  "lost".count AS "Perdues",
  "won".count  AS "Gagnées"
FROM (SELECT
        year([DOCUMENT_DATE])  AS "year",
        MONTH([DOCUMENT_DATE]) AS "month",
        COUNT(*)               AS "count"
      FROM [MY_TABLE]
      WHERE [STATUS] < 8 AND [DOCUMENT_DATE] >= ?1 AND [DOCUMENT_DATE] <= ?2 AND ([SEGMENT] = ?3 OR ?3 IS NULL)
      GROUP BY YEAR([DOCUMENT_DATE]), MONTH([DOCUMENT_DATE])) AS "open"
  FULL JOIN (SELECT
               year([DOCUMENT_DATE])  AS "year",
               MONTH([DOCUMENT_DATE]) AS "month",
               COUNT(*)               AS "count"
             FROM [MY_TABLE]
             WHERE [STATUS] = 8 AND [DOCUMENT_DATE] >= ?1 AND [DOCUMENT_DATE] <= ?2 AND ([SEGMENT] = ?3 OR ?3 IS NULL)
             GROUP BY YEAR([DOCUMENT_DATE]), MONTH([DOCUMENT_DATE])) AS "lost"
    ON "open".month = "lost".month AND "open".year = "lost".year
  FULL JOIN (SELECT
               year([DOCUMENT_DATE])  AS "year",
               MONTH([DOCUMENT_DATE]) AS "month",
               COUNT(*)               AS "count"
             FROM [MY_TABLE]
             WHERE [STATUS] > 8 AND [DOCUMENT_DATE] >= ?1 AND [DOCUMENT_DATE] <= ?2 AND ([SEGMENT] = ?3 OR ?3 IS NULL)
             GROUP BY YEAR([DOCUMENT_DATE]), MONTH([DOCUMENT_DATE])) AS "won"
    ON "open".month = "won".month AND "open".year = "won".year
ORDER BY CASE WHEN "open".year IS NOT NULL
  THEN
    "open".year
         ELSE
           (CASE WHEN "lost".year IS NOT NULL
             THEN
               "lost".year
            ELSE
              "won".year
            END)
         END,
  CASE WHEN "open".month IS NOT NULL
    THEN
      "open".month
  ELSE
    (CASE WHEN "lost".month IS NOT NULL
      THEN
        "lost".month
     ELSE
       "won".month
     END)
END

我很确定有一种更简单、更简洁的方法可以做到这一点,但我想不出来。

根据描述,我想这可能就是您要找的东西。

SELECT  year([DOCUMENT_DATE])  AS "year",
        MONTH([DOCUMENT_DATE]) AS "month",
        COUNT(case when [STATUS] > 8 then 1 end) win_count,    
        COUNT(case when [STATUS] = 8 then 1 end) lost_count,
        COUNT(case when [STATUS] < 8 then 1 end) open_count
FROM [MY_TABLE]
GROUP BY year([DOCUMENT_DATE]),MONTH([DOCUMENT_DATE])
ORDER BY 1,2

如果条件在所有计数中都是常见的,则添加 WHERE [DOCUMENT_DATE] >= ?1 AND [DOCUMENT_DATE] <= ?2 AND ([SEGMENT] = ?3 OR ?3 IS NULL)