解释不同的结果和 return 计数总和

Interpret different results and return sum of count

我有 2 个不同的 table,'category type' 在每个 table 中的存储方式不同。例如,在 table 1 中可能是 'Done',但在 table 2 中可能是 'Implemented'。我正在尝试从单个 [=41= 中汇总 2 的结果] 查询,以便我知道 4 个州中每个类别的状态:

  1. 完成
  2. 进行中
  3. 需要更多信息
  4. 积压

我是 运行 下面的查询,但是输出更改了 issue_status 的名称,但是,它不会 return 聚合结果。只需编辑输出的名称以匹配我告诉它的方式。有什么建议吗?

SELECT (case when issue_status in ('Done','Change Implemented','Assessed and scoped') then 'Done'
             when issue_status in ('In progress','In review','Scoping') then 'In progress'
             when issue_status in ('Additional information requested','Additional information required','More Information Required')  then 'Additional information requested'
             when issue_status = 'Backlog' then 'Backlog'
             end) as issue_status, count(*)
FROM raw.JIRA_ISSUES
             WHERE PROJECT_KEY in ('Project1', 'Project2')
and issue_status != ('Canceled')
and created_date > ('2021-09-03')
GROUP BY (case when issue_status in ('Done','Change Implemented','Assessed and scoped') then 'Done'
             when issue_status in ('In progress','In review','Scoping') then 'In progress'
             when issue_status in ('Additional information requested','Additional information required','More Information Required')  then 'Additional information requested'
             when issue_status = 'Backlog' then 'Backlog'
             end), issue_status
ORDER BY 2;

table(项目 1)的结构如下

Issue_status Date_created
Done 1/1/2021
In progress 1/1/2021

table(项目 2)的结构如下

Issue_status Date_created
Assessed and scoped 1/1/2021
Under review 1/1/2021

GROUP BY 使用别名有点冒险。

相反,请考虑通过 WITH 子句使用通用 table 表达式 (CTE) 来整理它。例如,

WITH subselect as (
  SELECT (case when issue_status in ('Done','Change Implemented','Assessed and scoped') then 'Done'
         when issue_status in ('In progress','In review','Scoping') then 'In progress'
         when issue_status in ('Additional information requested','Additional information required','More Information Required')  then 'Additional information requested'
         when issue_status = 'Backlog' then 'Backlog'
         end) as issue_status
   FROM raw.JIRA_ISSUES
         WHERE PROJECT_KEY in ('Project1', 'Project2')
     and issue_status != ('Canceled')
     and created_date > ('2021-09-03')
)
select subselect.issue_date,
       count(*)
  from subselect
 group by subselect.issue_date
 order by 2;

使用 WITH 的一个好处是您可以独立测试子查询,然后更好地了解它在 之前的样子计数和分组。这将帮助您检测查询中的疏忽。此外,一旦您巩固了该子查询,您就可以围绕它编写新的查询(例如,将 created_date 条件移动到外部查询中)并始终使该子查询处于原始工作状态。