如何从 sql 查询的单个日期列中获取 start/end 日期
How to get start/end date from single date column from an sql query
你能帮我解决我的问题吗?我用的是sybase数据库。
我有两个tabletrans_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 查询吗?在这里,我的描述一直都不标准。
您必须加入 trans_status Table 两次才能获得开始和结束日期。由于可能缺少结束日期,因此您必须将第二个连接设为左连接。尝试以下查询:
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 和 'Response Time in Hours' 将为 NULL。
那么你想要的是:
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和处理中日期
之间的差异
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 是否相同。
你能帮我解决我的问题吗?我用的是sybase数据库。
我有两个tabletrans_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 查询吗?在这里,我的描述一直都不标准。
您必须加入 trans_status Table 两次才能获得开始和结束日期。由于可能缺少结束日期,因此您必须将第二个连接设为左连接。尝试以下查询:
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 和 'Response Time in Hours' 将为 NULL。
那么你想要的是:
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和处理中日期
之间的差异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 是否相同。