-1

I have a sample data here:

enter image description here

And here is my desired output:

enter image description here

How can i get the distinct value based on min time?

here is the update i tried

enter image description here

myown email
  • 139
  • 1
  • 10

2 Answers2

0

A correlated subquery for filtering is probably the easiest solution:

select t.*
from t
where t.timestamp = (select min(t2.timestamp)
                     from t t2
                     where t2.id = t.id
                    );

You may need to take the date into account if you want the earliest record overall:

select t.*
from t
where (t.date, t.time) in (select t2.date, t2.time
                           from t t2
                           where t2.id = t.id
                           order by t2.date desc, t2.time desc
                          );

or if you want the earliest record on each date:

select t.*
from t
where t.timestamp = (select min(t2.timestamp)
                     from t t2
                     where t2.id = t.id and
                           t2.date = t.date
                    );
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

With MySQL 8.0, window functions are usually the most efficient way to proceed:

SELECT col1, col2, col3, col4, col5
FROM (
    SELECT t.*, ROW_NUMBER() OVER(PARTITION BY col2 ORDER BY col4) rn
    FROM mytable t
) x WHERE rn = 1

With earlier versions, I would use a NOT EXISTS condition with a correlated subquery:

SELECT *
FROM mytable t
WHERE NOT EXISTS (
    SELECT 1 FROM mytable t1 WHERE t1.col2 = t.col2 AND t1.col4 < t.col4 
)

Demo on DB Fiddle:

| col1 | col2 | col3       | col4     | col5       |
| ---- | ---- | ---------- | -------- | ---------- |
| 2    | AAA  | Customer 1 | 07:00:00 | 2019-03-04 |
| 3    | BBB  | Customer 2 | 15:00:00 | 2019-03-04 |

For this to perform efficiently, you would need an index on mytable(col2, col4):

CREATE INDEX mytable_idx ON mytable(col2, col4);

If you have more than one record with the same col1 and col2, you can add an additional criteria to avoid duplicates in the resultset, using column c1, which I understand is the primary key of the table:

SELECT *
FROM mytable t
WHERE NOT EXISTS (
    SELECT 1 
    FROM mytable t1 
    WHERE 
        t1.col2 = t.col2 
       AND (
           t1.col4 < t.col4 
           OR (t1.col4 = t.col4 AND t1.col1 < t.col1)
       )
)

Updated DB Fiddle.

GMB
  • 216,147
  • 25
  • 84
  • 135
  • sorry sir but did not get the output i need :( – myown email Mar 04 '19 at 01:57
  • @myownemail: please have a look at the [link to the db fiddle](https://www.db-fiddle.com/f/g6MPFw8MQA4zfgo8bZoW8P/2) that I provided in my answer. Given your sample data, these queries do produce the expected output. What is wrong, from your perspective? – GMB Mar 04 '19 at 02:00
  • our column 1 is different mine is unique, i try the second one and it took so long while the 1st one has an error but i think because of my low MYSQL version – myown email Mar 04 '19 at 02:06
  • @myownemail: column 1 does not make a difference in this case (I still updated my answer and the fiddle). – GMB Mar 04 '19 at 02:13
  • noted on that i will try again – myown email Mar 04 '19 at 02:13
  • So basically the second query is taking too long? This does not mean that it does not work... I added a recommendation in my answer to create an index. – GMB Mar 04 '19 at 02:14
  • tried the second one but the ouput is not the same as what i expected multiple columns instead of the unique based on min time – myown email Mar 04 '19 at 02:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189365/discussion-between-gmb-and-myown-email). – GMB Mar 04 '19 at 02:29