[英]Blank columns in results when there shouldn't be sqlite3
首先:我为我的愚蠢问题提前道歉。 我第一次使用 sqlite3 并且对 sql 生疏,所以我希望得到一些帮助。 我有一个小型的支持工单跟踪数据库,我想用它来创建指标。 我在使用我拥有的三个表格以可查看的格式提取信息时遇到了麻烦。
tickets ticket_changes ticket_history
==================================================
ticket_id history_id history_id
description ticket_id edited_by
created_by field_event
account_id old_value
contact_id new_value
product
我希望能够做的是搜索特定事件并提取有关该票证的详细信息。 例如,如果将票证分配给第 2 层团队,则票证历史记录中有该记录。 old_value 将是它被分配到的组,而 new_value 将是第 2 层。对于分配到第 2 层的票证,我想知道票证编号、打开它的时间、打开它的人等。理想情况下,结果看起来像这样:
ticket_number | created_by | created_date | tier2_assignment | product
===========================================================================
1234 jdoe 2018-01-01 00:00 2018-01-01 00:35 blue widget
1235 dsmith 2018-01-01 01:00 2018-01-02 12:01 green widget
我曾尝试使用CASE
pivot ,但我认为我的知识不足以使其发挥作用。
SELECT t.ticket_number, t.created_by,
CASE WHEN (th.field_event = 'Created.') THEN th.edit_date END AS created_date,
CASE WHEN (th.new_value = 'Tier 2') THEN th.edit_date END AS tier2_assignment,
c.type
FROM tickets AS c
JOIN ticket_changes AS tc ON c.ticket_id = tc.case_id
JOIN ticket_history AS th ON th.history_id = tc.history_id
WHERE th.new_value = 'Tier 2';
我的结果是空白的 created_by 和 created_date 列。
使这项工作的任何提示? 我很亲近吗? 是我不讲道理吗? 我在服用疯狂的药丸吗? 谢谢!
编辑:按要求模拟数据:
tickets
ticket_id created_by description account_id contact_id product
========================================================================================
5005000000o9rju jdoe Test 1 00150000016Vvvm 0035000002h4nli blue widget
5005000000wNy8l hgranger Test 2 0015000000nTLiI 0035000002urMIs green widget
50050000012kaoG wadama Test 3 0015000001Jc3DW 00350000031HSVD green widget
ticket_changes
history_id ticket_id
===============================
017500000U6HU4y 5005000000wNy8l
017500000U6HU4z 5005000000wNy8l
017500000U6ICyS 5005000000wNy8l
017500000MY0WoU 5005000000o9rju
017500000MY0WoV 5005000000o9rju
017500000MY0hhV 5005000000o9rju
017500000MY0hhW 5005000000o9rju
017500000cCVboC 50050000012kaoG
017500000cCVboD 50050000012kaoG
017500000cCVbsW 50050000012kaoG
017500000cIDT8t 50050000012kaoG
017500000cIDXp6 50050000012kaoG
ticket_history
history_id edited_by edit_date field_event old_value new_value
==============================================================================================
017500000cCVboC jdoe 2018-02-03 17:43:00 Created.
017500000cCVboD jdoe 2018-02-03 17:43:00 Status New Pending Support
017500000cCVbsW jdoe 2018-02-03 17:45:00 Status Pending Support Tier 2 Escalated
017500000cIDT8t dsmith 2018-02-05 09:01:00 Status Tier 2 Escalated Pending Support
017500000cIDXp6 dsmith 2018-02-05 09:13:00 Closed.
017500000U6HU4y hgranger 2017-07-10 15:02:00 Created.
017500000U6HU4z hgranger 2017-07-10 15:02:00 Status New Pending Support
017500000U6ICyS hgranger 2017-07-10 19:20:00 Closed.
017500000MY0WoU wadama 2016-08-22 12:28:00 Created.
017500000MY0WoV wadama 2016-08-22 12:28:00 Status New Pending Support
017500000MY0hhV wadama 2016-08-22 12:46:00 Closed. Pending Support Tier 2 Escalated
017500000MY0hhW ksmith 2016-08-22 12:46:00 Status Tier 2 Escalated Pending Support
017500000MY0hhW wadama 2016-08-22 13:16:00 Status Tier 2 Escalated Pending Support
我找到了您的问题。您可以使用like
和%
来查找您的new_value
数据。
编辑
需要将ticket_history
用where
子句作为子查询加入ticket_changes
。
因为需要通过where
子句来获取ticket_id
,
你得到ticket_id
可以加入ticket_history's history_id
然后你就会得到答案。
SELECT c.ticket_id,
c.created_by,
created_date.edit_date as created_date,
tier2_assignment.edit_date AS tier2_assignment,
c.product
FROM tickets AS c
JOIN ticket_changes AS tc ON c.ticket_id = tc.ticket_id
JOIN ticket_history AS th ON th.history_id = tc.history_id
INNER JOIN
(
SELECT edit_date,ticket_id
FROM ticket_history
INNER JOIN ticket_changes
ON ticket_history.history_id = ticket_changes.history_id
WHERE new_value LIKE '%Tier 2%'
) AS tier2_assignment ON tier2_assignment.ticket_id = tc.ticket_id
INNER JOIN
(
SELECT edit_date,ticket_id
FROM ticket_history
INNER JOIN ticket_changes
ON ticket_history.history_id = ticket_changes.history_id
WHERE field_event LIKE '%Created.%'
) AS created_date ON created_date.ticket_id = tc.ticket_id
WHERE th.new_value LIKE '%Tier 2%';
您可以查看链接示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.