0

I have below mentioned table (sample):

ID        Value           Date
TT-12     call rs         2018-04-01 15:18:22
TT-12     srte er         2018-04-02 12:15:18
TT-12     efft rs         2018-04-03 13:04:08
TT-14     efft rs         2018-04-04 17:16:10
TT-14     call rs         2018-04-05 16:25:43
TT-14     srte rs         2018-04-06 21:11:47
TT-18     srte rs         2018-04-07 22:18:34
TT-18     call rs         2018-04-08 07:11:35
TT-18     call rs         2018-04-09 13:07:25

From the above table i want to fetch only those rows which has Value Call rs against particular ID with oldest time.

Required Output:

ID        Value           Date
TT-12     call rs         2018-04-01 15:18:22
TT-14     call rs         2018-04-05 16:25:43
TT-18     call rs         2018-04-08 07:11:35

I am trying: select ID,Value,Date from Table1 where ID in ('TT-12','TT-14','TT-18') and order by Value='call rs';

Roy1245
  • 507
  • 4
  • 18
  • Possible duplicate of [SQL select only rows with max value on a column](https://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column) – Paul Spiegel May 16 '18 at 06:53

3 Answers3

0

Try this,

select ID, Value, min(Date) as date
from <Table>
where ID in ('TT-12','TT-14','TT-18') and Value = 'call rs'
group by ID, Value
KMS
  • 566
  • 4
  • 16
0

Do a self join to get the oldest row per ID

SELECT 
  a.* 
FROM
  table1 a 
  LEFT JOIN table1 b 
    ON a.ID = b.ID 
    AND a.`Date` > b.`Date` 
    AND b.`Value` = 'call rs' 
WHERE b.ID IS NULL 
  AND a.`Value` = 'call rs' 
  AND a.ID IN ('TT-12', 'TT-14', 'TT-18')

Demo

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0
select ID,Value,Date from Table1 where Value='call rs' group by ID order by Date desc;