Question

I have a table with several hundred thousand entries and I'm trying to use a query to get a result set for a specific receiver_id and group them by sender_id. My current SQL query works but I want to know if there could be any potential problems with using two MAX calls in the statement. It looks like this:

SELECT MAX(id) as id, sender_id, receiver_id, MAX(date) as date
FROM     messages
WHERE    receiver_id=5 and belong_to=5
GROUP BY sender_id

The table date looks like this:

id sender_id receiver_id content date                 belong_to           
-- --------- ----------- ------- -------------------  ---------
1  5         7           test    2013-03-11 10:33:54  7
2  5         7           test    2013-03-11 10:33:54  5
3  13        7           test 2  2013-03-13 12:01:36  7
4  13        7           test 2  2013-03-13 12:01:36  13
5  5         7           test 3  2013-03-14 09:15:37  7
6  5         7           test 3  2013-03-14 09:15:37  5
7  25        5           data1   2013-03-15 11:01:36  5
8  25        5           data1   2013-03-15 11:01:36  25
9  16        5           data2   2013-03-17 09:17:17  5
10 16        5           data2   2013-03-17 09:17:17  16
11 25        5           data3   2013-04-05 09:17:17  5
12 25        5           data3   2013-04-05 09:17:17  16

The output from my query is this:

id sender_id receiver_id date               
-- --------- ----------- -------------------
9  16        5           2013-03-17 09:17:17
11 25        5           2013-04-05 09:17:17

Are there any issues with this query using the MAX calls? If so what is the alternative?

Was it helpful?

Solution

I don't quite understand your structure (so, for instance, this example assumes that a UNIQUE key could be imposed upon sender_id, receiver_id, date, belong_to), but I suspect you want something like this. Filter by user as necessary..

SELECT x.* 
  FROM messages x
  JOIN 
     ( SELECT sender_id
            , receiver_id
            , MAX(date) max_date 
         FROM messages 
        GROUP 
           BY receiver_id
            , sender_id
     ) y 
    ON y.sender_id = x.sender_id 
   AND y.receiver_id = x.receiver_id 
   AND y.max_date = x.date
 WHERE x.belong_to = x.receiver_id;

OTHER TIPS

Based on comments what you want is:

' unique [list of sender_ids for a particular receiver_id with the most recent entry (date) for each[receiver]'

If you mean ALL Senders who have the latest entry date for that single receiver, then:

Select * From Messages m
Where date = (Select Max(date) From messages
              Where receiver_id = m.receiver_id) 
    And receiver_id = 5 -- add this if you only want results for one receiver_id

If otoh, you mean ' unique list of sender_ids for a particular receiver_id with the most recent entry (date) for each[receiver-sender combination]', then do this

Select * From Messages m
Where date = (Select Max(date) From messages
              Where Sender_id = m.Sender_id 
                  And receiver_id = m.receiver_id) 
    And receiver_id = 5 -- add this if you only want results for one receiver_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top