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

Mysql 如何从sql查询的单个日期列中获取开始/结束日期,mysql,sql,sybase,Mysql,Sql,Sybase,你能帮我解决我的问题吗。我用的是sybase数据库 我有两个表trans\u status,trans\u ref trans_状态:- | Corre_ID | Pro_type | Desc | Datetime | |----------|----------|-----------|-------------------------| | ABC_01 | Books | Started | 17/02/2016 00:17:18.9

你能帮我解决我的问题吗。我用的是sybase数据库

我有两个表trans\u status,trans\u ref

trans_状态:-

| 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 |
翻译参考:-

| 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表两次才能获得开始和结束日期。由于结束日期可能丢失,您必须将第二次连接设为左连接。请尝试以下查询:

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和“以小时为单位的响应时间”字段将为空。

因此,您需要的是:

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
如果我理解正确,那么响应时间就是开始日期和进程日期之间的差异

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

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

您必须解释实现该愿望的逻辑,我们可以尝试猜测,但这是浪费时间。请阅读,这里是一个很适合HI Juan的地方,谢谢你回答我的问题。我已经试了很多了。但仍然没有得到设计输出。如果您能帮助我了解您的技术专业知识,这将是一个很大的帮助。正如我所说,我无法帮助您,除非您解释获得该结果所需的逻辑,并阅读我提供的链接,以便您了解如何根据两个表制作更好的问题我希望从trans_status table获得更正id,并从trans_ref table获得相应的paymentId。此外,对于事务启动时的相关id,即min(日期),如果该相关id还有一条记录,则我需要max(日期)。但在结果中,我需要唯一的相关id,然后是付款id,开始日期为min(datetime),结束日期为max(datetime)。