sqlite3 query by max and filter by second factor

前端 未结 3 1423
醉梦人生
醉梦人生 2021-01-27 01:01

I have:

TABLE MESSAGES
 message_id | conversation_id | from_user | timestamp  |  message

I want:

1. SELECT * WHERE from_user &l         


        
3条回答
  •  醉话见心
    2021-01-27 01:46

    Try below sql to achieve your purpose by group by twice.

    select m.*
    from
    Messages m
    -- 3. and then joining to get wanted output columns
    inner join
    (
        --2. then selecting from this max timestamp - and removing duplicates
        select conversation_id, max(timestamp), message_id
        from
        (
            -- 1. first select max message_id in remainings after the removal of duplicates from mix of cv_id & timestamp
            select conversation_id, timestamp, max(message_id) message_id
            from Messages
            where message <> 'me'
            group by conversation_id, timestamp
        ) max_mid
        group by conversation_id
    ) max_mid_ts on max_mid_ts.message_id = m.message_id
    order by m.message_id;
    

    http://goo.gl/MyZjyU

提交回复
热议问题