Pergunta

I have a table that contains event information for users, i.e. each row contains an ID, a user's start date, completion date, and the event number. Example:

Row 1: ID 256 | StartDate 13360500 | FinishDate 13390500 | EventNum 3

I am trying to intersect all of the rows for users who have finished events 3 & 2, but I can't figure out why my query is returning no results:

SELECT table_id FROM table
    WHERE table_EventNum = 3 
    AND table_FinishDate > 0

    AND table_id IN (SELECT table_id FROM table WHERE table_EventNum = 2);

This query without the subquery (the line separated from the rest at the bottom) returns a bunch of non-null results, as it should, and the subquery also returns a bunch of non-null results (again, as it should). But for some reason the composite query doesn't return any rows at all. I know the IN command returns NULL if the expression on either side is NULL, but since both queries return results I'm not sure what else might cause this.

Any ideas? Thanks!

Foi útil?

Solução

Assuming FinishDate is NULL when the event is not complete. Also assuming that there has to be a row with matching id and event number 2 and that event 3 cannot happen before event 2:

SELECT t1.table_id FROM table t1 INNER JOIN table t2 ON t1.table_id = t2.table_id
    WHERE t1.table_EventNum = 3 AND t2.table_EventNum = 2
    AND NOT t1.table_FinishDate IS NULL

Note that I could not find anything wrong with your query other than the fact that you do not need a subquery.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top