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.
| Author |
Topic |
|
Russ1984
Starting Member
4 Posts |
Posted - 2008-10-16 : 06:13:42
|
| Hi,Can someone please help me with the following select statement. If I have the table:deviceStatusId, date, deviceId1, 01/01/01, 12, 02/01/01, 13, 01/01/01, 24, 02/01/01, 2If I have a table of deviceId's (from say: SELECT deviceId FROM PORTAL WHERE deviceId = @deviceId), returning Id's 1 & 2, how can I use this to select the deviceStatusId's that are most recently corresponding to these deviceId's. It's a bit of a mouthful but essentially I'm after deviceStatusId's 2 and 4 here. Maybe it's something like:SELECT deviceStatusId FROM deviceStatus WHERE (????) AND deviceId IN (SELECT deviceId from Device WHERE portalId = @portalId).Thanks in advance for any help I recieve.Russ. |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-10-16 : 06:24:19
|
Something like...SELECT a.[deviceStatusId]FROM ( SELECT MAX([date]) AS highest_Date , [deviceId] AS devId FROM <theTable> GROUP BY [deviceId] ) ms JOIN <theTable> a ON a.[deviceId] = ms.[devId] AND a.[date] = ms.[highest_date] -------------Charlie |
 |
|
|
|
|
|
|
|