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 |
|
label
Posting Yak Master
197 Posts |
Posted - 2008-01-18 : 12:41:40
|
I have the following result set: KeyID Linked_id 678 NULL678 1679 NULL679 1680 NULL680 1681 NULL681 1682 NULL682 1683 NULL684 NULL685 NULL686 NULL687 NULL My business rules: In a single select statement I want to: 1. Bring back 1 of each keyID2. If two of the same keyId exist in the results, and one of the LinkedID's has a value and the other is null, I want to use the linkedID that has a value. I've tried an inner join on itself without success. Any thoughts? |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2008-01-18 : 12:47:13
|
| Do a group by on KeyId and a MIN(Linked_id) and see if you get the results you are looking for..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-18 : 12:56:08
|
| [code]SELECT t.KeyID,MAX(t.Linked_id) AS Linked_idFROM(SELECT KeyID,ISNULL(Linked_id,0) AS Linked_id FROM Table)tGROUP BY t.KeyID[/code] |
 |
|
|
label
Posting Yak Master
197 Posts |
Posted - 2008-01-18 : 13:14:51
|
quote: Originally posted by visakh16
SELECT t.KeyID,MAX(t.Linked_id) AS Linked_idFROM(SELECT KeyID,ISNULL(Linked_id,0) AS Linked_id FROM Table)tGROUP BY t.KeyID
Thanks, that worked perfectly! |
 |
|
|
|
|
|