如何从sql查询中的单个日期列获取开始/结束日期

时间:2016-02-17 14:28:42

标签: mysql sql sybase

你能帮我解决一下我的问题吗?我正在使用sybase数据库。

我有两个表trans_status,trans_ref

trans_status: -

| Corre_ID | Pro_type |      Desc |                Datetime |
|----------|----------|-----------|-------------------------|
|   ABC_01 |    Books |   Started | 17/02/2016 00:17:18.963 |
|   ABC_01 |    Books | Inprocess | 17/02/2016 00:18:18.963 |
|   ABC_01 |    Books |  Finished | 17/02/2016 00:19:18.963 |
|   ABC_02 |     XXXX |   Started | 16/02/2016 00:17:18.963 |
|   ABC_02 |     XXXX | Inprocess | 16/02/2016 00:18:18.963 |
|   ABC_02 |     XXXX |  Finished | 16/02/2016 00:19:18.963 |
|   ABC_03 |     yyyy |   Started | 15/02/2016 00:17:18.963 |
|   ABC_03 |     yyyy | Inprocess | 15/02/2016 00:18:18.963 |
|   ABC_03 |     yyyy |  Finished | 15/02/2016 00:19:18.963 |
|   ABC_04 |     zzzz |   Started | 14/02/2016 00:19:18.963 |

trans_ref: -

| Payment_ID | Corre_ID |
|------------|----------|
|       1111 |   ABC_01 |
|       2222 |   ABC_02 |
|       3333 |   ABC_03 |
|       4444 |   ABC_04 |

期望的输出: -

Corre_ID-----Payment_ID-----StartDate-----EndDate-----Response Time in Hours
ABC_01-----1111-----17/02/2016 00:17:18.963-----17/02/2016 00:19:18.963-----1
ABC_02-----2222-----16/02/2016 00:17:18.963-----16/02/2016 00:19:18.963-----1
ABC_03-----3333-----15/02/2016 00:17:18.963-----15/02/2016 00:19:18.963-----1
ABC_04-----4444-----14/02/2016 00:19:18.963-----EMPTY-----EMPTY

请你帮我构建一个sql查询。在这里,我的描述一直不标准。

3 个答案:

答案 0 :(得分:0)

您必须两次加入trans_status表才能获得开始日期和结束日期。由于可能缺少结束日期,因此您必须使第二个连接成为左连接。请尝试以下查询:

SELECT
  r.Corre_ID,
  Payment_ID,
  s_start.Datetime AS StartDate,
  s_end.Datetime AS EndDate,
  TIMEDIFF(s_end.Datetime, s_start.Datetime) AS 'Response Time in Hours'
FROM
  trans_ref r JOIN trans_status s_start ON (r.Corre_ID = s_start.Corre_ID AND s_start.Desc = 'Started')
              LEFT JOIN trans_status s_end ON (r.Corre_ID = s_end.Corre_ID AND s_end.Desc = 'Finished')

如果该过程未完成,则EndDate字段和“以小时为单位的响应时间”将为NULL。

答案 1 :(得分:0)

所以你想要的是这个:

SELECT t.Corre_ID,s.Payment_ID,
       max(case when t.Desc = 'Started' then t.Datetime end) as startDate,
       max(case when t.Desc = 'Finished' then t.Datetime end) as startDate,
       TIMESTAMPDIFF(HOUR,max(case when t.Desc = 'Started' then t.Datetime end),
                     max(case when t.Desc = 'Inprocess' then t.Datetime end)) as response
FROM trans_status t 
INNER JOIN trans_ref s ON (t.Corre_ID = s.Corre_ID)
GROUP BY t.Corre_ID,s.Payment_ID

如果我理解正确,那么响应时间就是start_date和inprocess date之间的差异

答案 2 :(得分:0)

SELECT sd.Corre_ID, sd.Payment_ID, sd.Datetime StartDate, ed.Datetime EndDate, 
    TIMESTAMPDIFF(HOUR, ed.EndDate, sd.StartDate) `Response Time in Hours` 
FROM
    (SELECT ts.Corre_ID, tr.Payment_ID, ts.Datetime
    FROM trans_status ts
    JOIN trans_ref tr on ts.Corre_ID = tr.Corre_ID
    WHERE ts.Desc = 'Started'
    GROUP BY ts.Corre_ID) sd
    LEFT JOIN 
    (SELECT ts.Corre_ID, tr.Payment_ID, ts.Datetime
    FROM trans_status ts
    JOIN trans_ref tr on ts.Corre_ID = tr.Corre_ID
    WHERE ts.Desc = 'Finished'
    GROUP BY ts.Corre_ID) ed
    on sd.Corre_ID = ed.Corre_ID

PS。这是MySQL语法,不确定它是否与Sybase相同。