S
S
sbh2020-05-03 09:13:10
MySQL
sbh, 2020-05-03 09:13:10

How to select unique records with the maximum value for a specific user?

Table1

ID   ClientID    StartTS                               FinishTS
1     2233        2020-05-01 12:02:01                  2020-05-01 13:02:03
2     2233        2020-05-01 12:02:01                  2020-05-01 14:02:03
3     5212        2020-05-01 12:02:01                  2020-05-01 11:01:11
4     2233        2020-05-02 08:02:11                  2020-05-02 09:01:11
5     2233        2020-05-02 08:02:11                  2020-05-02 10:01:12
6     5212        2020-05-01 10:01:32                  2020-05-01 12:01:11


It is necessary to select all records with a unique StartTS field and the maximum of those selected for each FinishTS record by user 2233.

That is, in this example, the correct query result will be:
ID   ClientID    StartTS                               FinishTS
2     2233        2020-05-01 12:02:01                  2020-05-01 14:02:03
5     2233        2020-05-02 08:02:11                  2020-05-02 10:01:12

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sbh, 2020-05-03
@sbh

SELECT `ClientID`,`StartTS`, MAX(`FinishTS`) FROM `Table1` where `ClientID` = '2233' group by `StartTS` - this seems to work correctly and quickly, correct if you see an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question