Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have A table name
Patient
ptn_no ipno admission_date
1 1 12/12/11
1 2 14/12/11


i want the latest entry in this table for ptn_no 1.

Help me for the Sql Query
how can i match the latest entry through admission date
Posted

If you have a primary key or unique identity column, just sort by that descending and you will get the latest record right on top.
 
Share this answer
 
Comments
ankur789 4-Dec-12 23:04pm    
i Want only the latest single row in the entries
please provide sql query for it
Espen Harlinn 11-Dec-12 17:52pm    
Good point :-D
Abhinav S 12-Dec-12 0:53am    
Thanks Espen.
To get the latest record using the admission date column, the query can be built in several different ways.

One way is to select it using a non-correlated condition, for example:
SQL
SELECT *
FROM Patient
WHERE Admission_Date = (SELECT MAX(Admission_Date) FROM Patient)

The above will work only if there is no duplicates with the admission date.

Another option could be to use TOP, for example:
SQL
SELECT TOP(1)
       ptn_no,
       admission_date
FROM Patient
ORDER BY admission_date DESC

Again if there are duplicates for admission date, you will basically get a random record for the latest admission date.
 
Share this answer
 
Comments
Espen Harlinn 11-Dec-12 17:52pm    
5'ed!
Wendelius 12-Dec-12 0:16am    
Thank you :)
get records using desc order of admission date and ipno in case of similar date it will give you latest entry by ipno
SQL
SELECT top 1 *
FROM Patient 
where ptn_no = 1 
ORDER BY admission_date, Ipno DESC

Happy Coding!
:)
 
Share this answer
 
Comments
Espen Harlinn 11-Dec-12 17:51pm    
5'ed!
Aarti Meswania 11-Dec-12 20:43pm    
thank you! :)

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