Search code examples
sqloracle-databasegreatest-n-per-group

about the array in condition


Table1: https://i.sstatic.net/XVyYf.png

Table2: https://i.sstatic.net/762uM.png

expect the result: https://i.sstatic.net/lNssy.png

if have many data in array[R80,R01,R02,R03,R04,R05...] and 'Table2', so i want in array to find the 'Table2' data the result must 'date' is new(DESC)/limit 1

example:

like select * from tabel2 where ID = 'R80' DESC Date Limit1

But i want to get all items [R80,R01,R02,R03,R04,R05...]

like select * from tabel2 where in [R80,R01,R02,R03,R04,R05...] DESC Date Limit1??

Please help~Thx

not use "loop,declare,@" is best

Please dont answer 
select * from tabel2 where ID = 'R80'...;
select * from tabel2 where ID = 'R01'...;
select * from tabel2 where ID = 'R02'...;
                   ...

Solution

  • Seems like you are looking for top 1 row for all IDs based on latest date. Use row number

    SELECT * FROM (
        --mark 1st row as 1 for every ID
        SELECT *,row_number() over(partition by ID order by date desc) RN from table2
        where id in ('R80','R01','R02','R03','R04','R05') 
    ) INNER_QUERY 
    WHERE RN = 1