Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Selecting most recent records

Author  Topic 

bannerke
Starting Member

1 Post

Posted - 2014-05-25 : 06:55:30
Hi,

I have a DB with the fields: Id, Date, Name, Status.

Now I want to select all distinct names with the most recent corresponding status.

With the SELECT DISTINCT Name, I get only one record per different name which is ok.
Now I want to add next to each different name, the most recent added status.
Can I combine those 2 in 1 query? I want to show them using PHP on a website.
Now I get a table with a row for each different name, but I can't add a second column where the most recent Status is showed for that name.

Can somebody help me?

Thanks!

GouravSaxena1987
Starting Member

23 Posts

Posted - 2014-05-25 : 15:34:49
Hello,

I am assuming that you have one date column(ModifiedDate) for check what is the recent record so,

Here is your query:

SELECT DISTINCT Name,Status from TableName Where MOdifiedDate in (SELECT MAX(ModifiedDate) FROM TableName GROUP BY Name)



Regards,
Gourav Saxena
Data Warehouse Counsultant
GouravSaxena1987@gmail.com
Go to Top of Page
   

- Advertisement -