简体   繁体   中英

SQL Query to identify missing Event (record)

I have a table events where I store Cust-ID, Event-ID, Time

My Events are fixed with IDs as EV1, EV2, EV3, EV4, EV5 (in sequential order but the sequence of EV2 and EV3 can be repeated and also that EV3 cannot happen without EV2)

I want to have a query where I can find the details of Cust-ID and the time where EV2 was missed ie, EV3 was recorded without a preceding EV2. For eg, if there is data like the table below, I want to identify the output as 1, EV3, 10:30 as EV2 is missing.

------------------------------------
| Cust_ID    | Event_ID |time      |
------------------------------------
| 1          | EV1      | 10:29    |
------------------------------------
| 1          | EV3      | 10:30    |
------------------------------------
| 1          | EV4      | 10:31    |
------------------------------------
| 1          | EV5      | 10:33    |
------------------------------------

SELECT Cust-Id,time FROM “ Your-table ” WHERE EV2 IS NULL, EV3 IS NOT NULL

This should filter out the cust-id and time, where there is no value for EV2 but EV3 exists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM