visid,custid和value的每个concactentation可能有两个"细节"并且"确认"或只是"细节"。 如果它只有" detail",则不应检索那些行,否则我必须找到日期时间之间的差异 当visid,custid和value的组合同时具有"细节"和"确认"
visid custid value datetime value1
123 456 11 2017-03-01 12:34:11 Detail
123 456 11 2017-03-01 12:36:11 confirm
567 342 56 2017-03-01 12:45:11 Detail
567 342 56 2017-03-01 12:46:11 confirm
411 124 78 2017-03-01 12:34:11 Detail
Output:
visid custid value datetime
123 456 11 00:02:00 (12:36:11 - 12:34:11)
567 342 56 00:01:00 (12:46:11 - 12:45:11)
注意:由于411,124和78的最后一行没有确认,因此必须删除该行。
我试过查询:
select visid, custid, value, concat(visid, custid, value) as session, datetime
from table1
where datetime between "2017-05-01 00:00:00" and "2017-05-02 00:00:00"
and value1 in ('Detail','Confirm')
答案 0 :(得分:0)
datetime
函数是特定于数据库的。在您的情况下,自联接是一种简单的方法:
select td.visid, fd.custid, td.value, concat(visid, custid, value) as session,
(tc.datetime - td.datetime) as diff
from table1 td join
table1 tc
on td.visid = tc.visid and td.custid = tc.custid and td.value = tc.value and
td.value1 = 'Detail' and tc.value1 = 'Confirm';
这使用减法来表示差异。确切的功能取决于数据库。