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 |
|
carlo88
Starting Member
4 Posts |
Posted - 2007-04-10 : 17:43:37
|
| I have two separate queries:1.SELECT TOP 1 nDeviceGroupID FROM PivotDeviceToGroup WHERE PivotDeviceToGroup.nDeviceID = 102.SELECT sGroupName FROM DeviceGroup WHERE nDeviceGroupID = 28 The first retrieves the nDeviceGroupID i.e. (28). I would like to join these two queries. Where the output of the first i.e. (28) is the input to the second query. In the end I would like to retrieve the sGroupName based on a specific nDeviceID. cheers,carlo |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-10 : 17:49:28
|
| (1) may not always retirve 28 because there is no ORDER BY. so SQL Server will just pick any row (the first it can find) that matches the conditions.Looking for something like this:SELECT sGroupName FROM DeviceGroup WHERE nDeviceGroupID = (SELECT TOP 1 nDeviceGroupID FROM PivotDeviceToGroup WHERE PivotDeviceToGroup.nDeviceID = 10 ORDER BY somecolumn )If you remove the TOP 1, the query might fail because the WHERE condition is expecting just one value. If your subquery returns more than one value you would have to use IN instead of the "=".************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
carlo88
Starting Member
4 Posts |
Posted - 2007-04-10 : 17:57:35
|
| Good point dinkara. The nDeviceGroupID (28) correspond to the parent folder. The parent folder may be in another folder. Hence the possibility of retrieving a few ID's. I basically just want to first parent folder, which seems to be always the lowest number. And thank you for concatenating the query together. Works great. |
 |
|
|
|
|
|