Click here to Skip to main content
15,886,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir i have two table

Table-1:

ID - Name
1 - Deepak
2 - Arun

Table-2:

ID - status - Date
1 - 'ok' - '10-Apr-2018'
1 - 'not ok' - '12-Apr-2018'
2 - 'ok' - '15-Apr-2018'
2 - 'ok' - '20-Apr-2018'

The first Table Id save in Table2 multiple time. i need a result from Table2 which have max date. and result look like this:

Result:

ID - Name - Status -Date
1- Deepak- 'not ok' -'12-Apr-2018'
2- Arun - 'ok' -'20-Apr-2018'


could someone help me.

What I have tried:

i am trying using max date but its not working for me.
Posted
Updated 21-Aug-18 3:51am
Comments
Richard MacCutchan 21-Aug-18 10:46am    
"i am trying using max date but its not working for me."
Sorry, but people here cannot guess the content of your SELECT statement, or what "not working" means.
TCS54321 22-Aug-18 0:47am    
tnx for your comment
Richard MacCutchan 22-Aug-18 3:12am    
That's OK, but we still have no idea what your code does or what "not working" actually means.

1 solution

You can use Row_number function for this:
SQL
SELECT x.Id, x.Name, x.Status, x.Date
FROM
(
     SELECT ROW_NUMBER() OVER (PARTITION BY T2.ID ORDER BY T2.Date DESC) as RowNo,
            T2.Id, T1.Name, T2.Status, T2.Date
     FROM Table-2 as T2 JOIN Table-1 as T1 on T2.Id=T1.Id
)
AS x
WHERE RowNo=1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900