1

I have a table that holds the answers to a question which is asked at entry to the system, at review periods and then at closure. The client can be opened and closed multiple times during their life on the system.

I am trying to get the latest 'entry' result from the table which also has either an associated 'review' or 'close' result.

This is my table (I have just included 1 user but the actual table has thousands of users):

row | user_id | answer | type   | date_entered |
----+---------+--------+--------+--------------+
 1  |   12    |   3    | entry  | 2016-03-13   |
 2  |   12    |   1    | review | 2016-03-14   |
 3  |   12    |   7    | review | 2016-03-16   |
 4  |   12    |   7    | close  | 2016-03-17   |
 5  |   12    |   8    | entry  | 2016-03-20   |
 6  |   12    |   2    | review | 2016-03-21   |
 7  |   12    |   3    | close  | 2016-03-22   |
 8  |   12    |   1    | entry  | 2016-03-28   |

So for this table the query would just return row 5 because the 'entry' on row 8 doesn't have any 'review' or 'closure' records after it.

Hopefully that makes sense.

5
  • The answer doesn't really have anything to do with it, I just put that in as an example column which holds the answer to the question asked, sorry if that has confused things. It is all to do with the type column, if there are no records that have review or close in the type column, after a record that has entry in the type column it isn't included. There has to be a record that has review or close in the type column after (by date) the entry record for it to be counted.
    – Sean
    Aug 2, 2016 at 14:24
  • How can you tell that rows 6 & 7 relate to row 5?
    – Strawberry
    Aug 2, 2016 at 14:42
  • They are all related because they are all for the same user.
    – Sean
    Aug 2, 2016 at 14:42
  • So, no user can ever have more than one active entry?
    – Strawberry
    Aug 2, 2016 at 14:54
  • No they always have an entry, then 0 or more reviews then a close. Then the process can start again. There will never be an entry record before there has first been a close, or it is the very first record.
    – Sean
    Aug 2, 2016 at 14:56

3 Answers 3

1
SELECT a.* 
  FROM my_table a 
  JOIN 
     ( SELECT x.user_id
            , MAX(x.date_entered) date_entered 
         FROM my_table x 
         JOIN my_table y 
           ON y.user_id = x.user_id 
          AND y.date_entered > x.date_entered 
          AND y.type IN ('review','close') 
        WHERE x.type = 'entry' 
        GROUP 
           BY x.user_id 
     ) b 
    ON b.user_id = a.user_id 
   AND b.date_entered = a.date_entered;
0
0

Basically you can seperate your query into two sub-queries. First query should get lastest record id (review and closure). Second query should have row_id > found_id.

SELECT * 
FROM   my_table 
WHERE  type = 'entry' 
       AND row_id > (SELECT Max(row_id) 
                     FROM   my_table 
                     WHERE  ( type = 'review' 
                               OR type = 'close' )) 

Please be careful about that; subquery may return zero-set.

0

I could think of several ways of doing it. But first a note: your date_entered field seems to be just a date. To tell which occurs "later" I'm going to use row because e.g. if both entry and review occurred on the same date, it's not possible to tell from the date_entered which one was later.

I just list a couple of solutions. The first one might be more efficient, but you should measure.

Here's a join against a subquery:

SELECT
    m1.*
FROM
    mytable m1
    JOIN (SELECT
              row, user_id
          FROM
              mytable
          WHERE
              type IN ('review', 'close') AND
              user_id = 12
          ORDER BY row DESC LIMIT 1) m2 ON m1.user_id = m2.user_id
WHERE
    m1.user_id = 12 AND
    m1.row < m2.row
ORDER BY
    row DESC LIMIT 1

Here's a subquery for max:

SELECT
    *
FROM
    mytable
WHERE
    row = (SELECT
               MAX(m1.row)
           FROM
               mytable m1,
               mytable m2
           WHERE
               m1.user_id = m2.user_id AND
               m1.type = 'entry' AND
               m2.type IN ('review', 'close') AND
               m1.row < MAX(m2.row))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.