postgresql - 在 pgsql 中获得类似枢轴的操作
问题描述
我正在尝试根据错误 ID 以及它们何时更改来获取错误的状态。关于使用以下查询:
select b.bug_id,b.creation_ts as issue_reported,ba.added,ba.bug_when
from bugs b
join bugs_activity ba on b.bug_id = ba.bug_id
join fielddefs fd on ba.fieldid = fd.id
where fd.name = 'bug_status' order by b.bug_id
我得到结果集(子集如下)
bugid issue_reported added bug_when
61 06-06-2019 9:00 Confirmed 06-06-2019 9:00
61 06-06-2019 9:00 In_Progress 06-06-2019 10:00
61 06-06-2019 9:00 Resolved 06-06-2019 15:00
61 06-06-2019 9:00 Verified 06-07-2019 11:00
如何修改我的查询以获得如下结果集:
bugid issue_reported Confirmed In_Progress Resolved Verified
61 06-06-2019 9:00 06-06-2019 9:00 06-06-2019 10:00 06-06-2019 15:00 06-07-2019 11:00
我的意思是单排。
解决方案
在您现有的查询之上,您想要 GROUP BY bug_id, issue_reported
,并将行转换为具有这种排列的列:
SELECT bugid,
issue_reported,
max(case when added='Confirmed' then bug_when end) AS "Confirmed",
max(case when added='In_Progress' then bug_when end) AS "In_Progress",
... repeat this for each value of "added" to transpose to rows
FROM (
-- the query from your question as a subquery
select b.bug_id,b.creation_ts as issue_reported,ba.added,ba.bug_when
from bugs b
join bugs_activity ba on b.bug_id = ba.bug_id
join fielddefs fd on ba.fieldid = fd.id
where fd.name = 'bug_status'
) subq
GROUP BY bugid, issue_reported
ORDER BY 1;
推荐阅读
- python - Python Smtplib WinError 100022
- python - 新版本 Python 在旧版本 Python 上运行 TKINTER & TURTLE
- python - 将图像分割成高频和低频
- xml - 具有多个命名空间的 XML 文件 - 如何在混合它们时定义有效位置
- ansible - Ansible SNOW 模块
- c - 根据一些特性编辑代码
- r - 将 DF 中的值从一行粘贴到一系列列中的另一行
- c# - 订阅期满后如何限制?
- python - 使用 python-eve 服务器对程序进行分析
- react-native - Shopify create customer api 正在使用邮递员,但不适用于反应本机应用程序