INFO: I am working with Microsoft SQL.
Ok the title is confusing but here is an example of the table I'm working with:
ID Value Signal Read Firmware Date Time
5 123 656 444 217 3/30/2009 11:00:00 AM
5 123 421 333 217 3/30/2009 04:00:00 PM
5 123 111 666 217 3/30/2009 05:00:00 PM
9 321 231 551 216 3/30/2009 09:00:00 AM
9 321 599 887 216 3/30/2009 09:30:00 AM
So I want the Query to return:
ID Value Signal Read Firmware Date Time
5 123 111 666 217 3/30/2009 05:00:00 PM
9 321 599 887 216 3/30/2009 09:30:00 AM
I have tried:
SELECT DISTINCT ID, Value, Signal, Read, Firmware, Date, Time FROM ....
But this returns all of the results. I have also tried SELECT TOP 1... but I couldn't get that to work. I know this is simple, but I'm confused on how to get this to display only 1 single unique row.
Thanks for the help.
Have you tried this?
The records are distinct, there are different
Signal
,Read
andTime
values. How would you expect the SQL server to guess which one you'd like?Your example suggests that you're interested in the most recent record of a given
Id
. If that's true, this query should work for you:Assuming that there is a check so that records with the same Id also have same Value
Similar query (uses CROSS APPLY and correlated subquery w/ TOP WITH TIES):
use TOP 1 WITH TIES