db2 - select rows with conditions in sql -
each concactentation of visid, custid , value might have both "detail" , "confirm" or "detail". if has "detail", rows should not retrieved, else have find difference between datetimes when combination of visid, custid , value have both "detail" , "confirm"
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) note: since last row 411, 124 , 78 not have confirm, row has eliminated.
query tried:
select visid, custid, value, concat(visid, custid, value) session, datetime table1 datetime between "2017-05-01 00:00:00" , "2017-05-02 00:00:00" , value1 in ('detail','confirm')
datetime functions database specific. in case, self-join simple approach:
select td.visid, fd.custid, td.value, concat(visid, custid, value) session, (tc.datetime - td.datetime) diff table1 td join table1 tc on td.visid = tc.visid , td.custid = tc.custid , td.value = tc.value , td.value1 = 'detail' , tc.value1 = 'confirm'; this uses subtraction difference. exact function depends on database.
Comments
Post a Comment