简体   繁体   中英

MYSQL query that can group by the highest datetime from two rows that might have the same value but flip flopped

I have a MYSQL 5.x database that I can't change the schema, read only access:

SELECT * FROM table;

--------------------+------------+------------+---------
stamp               | src        | dst        | data      
--------------------+------------+------------+---------
2017-12-06 11:08:39 | 2055551234 | 2059999999 | somedata
2017-12-04 16:04:14 | 2059999999 | 2055551234 | somedata
2017-12-04 14:28:21 | 2055551234 | 2059999999 | somedata
2017-12-04 14:26:24 | 8004441111 | 2562428766 | somedata
2017-12-04 14:06:22 | 2562428766 | 8004441111 | somedata
2017-12-04 13:15:52 | 8004441111 | 2562428766 | somedata
+---------------------+------------+------------+-------

I need all rows that service sets of two numbers, for example 2055551234 and 2055551234, each can be SRC or DST. I need the most recent row.

I need rows that contain both 2055551234 and 2055551234 but in either SRC or DST columns. The values can be in either column and thus my problem.

Each of the matching SRC,DST sets has multiple rows, I need the row with the most recent datetime from each set.

I thought I had it outsmarted when I decided to try to create a hash with MD5(SRC+DST) which would also equal MD5(DST+SRC) but I got unreliable results.

There can be anywhere from 1 to thousands of groups of rows that have matching sets.

I need to be able to LIMIT the results so just iterating client side isn't going to work because of the unknown rows in each group.

I can't store any procedures, any iterating work will have to be done client side.

From the above data I would like:

--------------------+------------+------------+---------
stamp               | src        | dst        | data      
--------------------+------------+------------+---------
2017-12-06 11:08:39 | 2055551234 | 2059999999 | somedata
2017-12-04 14:26:24 | 8004441111 | 2562428766 | somedata
+---------------------+------------+------------+-------

Thoughts?

Ah, now I think I got it. You are not looking for two particular numbers; you are looking for groups of rows that have the same number pair, no matter in what order. So get the lesser and the greater value and group.

select *
from mytable
where (least(src, dst), greatest(src, dst), stamp) in
(
  select least(src, dst), greatest(src, dst), max(stamp)
  from mytable
  group by least(src, dst), greatest(src, dst)
);

SQL fiddle: http://sqlfiddle.com/#!9/5bed58/1

SELECT *
FROM mytbl AS t1
WHERE stamp IN (
    SELECT MAX(stamp) AS maxStamp
    FROM (
        SELECT *, CONCAT(GREATEST(src, dst), '.', LEAST(src, dst)) AS gbval 
        FROM `mytbl` AS t2
    ) AS t3
    GROUP BY t3.gbval
    HAVING maxStamp = t1.stamp
)

Does this work?

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