结果中的空白列不应该是sqlite3

时间:2018-02-06 00:39:56

标签: sql sqlite

首先:我提前为我的愚蠢问题道歉。我是第一次使用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层团队,则在ticket_history中有记录。 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进行数据透视,但我认为我不太了解它是否有效。

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

1 个答案:

答案 0 :(得分:0)

我找到了您的问题。您可以使用like%来查找您的new_value数据。

修改

需要ticket_historyticket_changes子句作为子查询加入where

因为您需要通过ticket_id子句获取where

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%';

您可以看到链接示例

SQLFiddle Sample