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 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-07 : 20:56:11
|
I have two table looks like belowTable AId object_key1001 1111002 1891003 1456 Table BID Reason_cd labor_id object_key1001 1 2 981001 5 11 1111002 1 34 1891003 1 45 12341003 2 12 12371003 5 43 13451003 5 23 1456 I am trying to join TAble A and B using object_key and ID..but if the matching object_key and ID in table B have reson_cd 5 then .. I need to look for the latest record (by object_key) where reason_Cd <> 5 ID A.object_key B.ObjecT_key reason_cd labor_id 1001 111 98 1 21002 189 189 1 341003 1456 1237 2 12 How can I do join conditionally like this? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-08 : 04:51:28
|
| [code]DECLARE @TableA table(Id int,object_key int)INSERT INTO @TableASELECT 1001 , 111UNION ALLSELECT 1002 , 189UNION ALLSELECT 1003, 1456DECLARE @TableB table(ID int, Reason_cd int,labor_id int, object_key int)INSERT INTO @TableBSELECT 1001, 1, 2, 98UNION ALLSELECT 1001, 5, 11 , 111UNION ALLSELECT 1002, 1 , 34, 189UNION ALLSELECT 1003, 1, 45, 1234UNION ALLSELECT 1003, 2, 12, 1237UNION ALLSELECT 1003, 5, 43, 1345UNION ALLSELECT 1003, 5, 23, 1456SELECT a.ID, a.object_key,COALESCE(b.object_key,tmp.object_key),COALESCE(b.Reason_cd,tmp.Reason_cd),COALESCE(b.labor_id,tmp.labor_id) FROM @TableA aLEFT JOIN @TableB bON a.Id =b.IDAND a.object_key=b.object_keyAND b.Reason_cd <>5OUTER APPLY (SELECT b1.* FROM @TableB b1 INNER JOIN (SELECT ID,MAX(object_key) AS object_key FROM @TableB WHERE Reason_cd<>5 GROUP BY ID)t ON t.ID=b1.ID AND t.object_key = b1.Object_key WHERE b1.ID=a.ID)tmpOutput------------------------------------ID object_key Bobject_key Reason_cd labor_id----------- ----------- ----------- ----------- -----------1001 111 98 1 21002 189 189 1 341003 1456 1237 2 12 [/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-08 : 05:44:05
|
Using same sample data as Visakh;WITH Yak (ID, Object_Key, RecID, Reason_CD, Labor_ID)AS ( SELECT ID, Object_Key, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Object_Key DESC), Reason_CD, Labor_ID FROM @TableB WHERE Reason_CD <> 5)SELECT y.ID, t.Object_Key, y.Object_Key, y.Reason_CD, y.Labor_IDFROM Yak AS yINNER JOIN @TableA AS t ON t.ID = y.IDWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-08 : 05:45:26
|
And if you prefer not using CTESELECT y.ID, t.Object_Key, y.Object_Key, y.Reason_CD, y.Labor_IDFROM ( SELECT ID, Object_Key, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Object_Key DESC) AS RecID, Reason_CD, Labor_ID FROM @TableB WHERE Reason_CD <> 5 ) AS yINNER JOIN @TableA AS t ON t.ID = y.IDWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-08 : 08:46:21
|
| Thank you very much Peso and visakh16 |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-08 : 13:17:11
|
quote: Originally posted by Peso Using same sample data as Visakh;WITH Yak (ID, Object_Key, RecID, Reason_CD, Labor_ID)AS ( SELECT ID, Object_Key, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Object_Key DESC), Reason_CD, Labor_ID FROM @TableB WHERE Reason_CD <> 5)SELECT y.ID, t.Object_Key, y.Object_Key, y.Reason_CD, y.Labor_IDFROM Yak AS yINNER JOIN @TableA AS t ON t.ID = y.IDWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16"
peso -- problem is i still would like to join by objecT_key if the matching record ( join by object_key) from table B is not 5 ( it can be 1,2,3,4)..You code only show the latest mathcing record from table b where reason_cd <> 5.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-08 : 13:28:12
|
quote: Originally posted by jung1975
quote: Originally posted by Peso Using same sample data as Visakh;WITH Yak (ID, Object_Key, RecID, Reason_CD, Labor_ID)AS ( SELECT ID, Object_Key, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Object_Key DESC), Reason_CD, Labor_ID FROM @TableB WHERE Reason_CD <> 5)SELECT y.ID, t.Object_Key, y.Object_Key, y.Reason_CD, y.Labor_IDFROM Yak AS yINNER JOIN @TableA AS t ON t.ID = y.IDWHERE RecID = 1 E 12°55'05.25"N 56°04'39.16"
peso -- problem is i still would like to join by objecT_key if the matching record ( join by object_key) from table B is not 5 ( it can be 1,2,3,4)..You code only show the latest mathcing record from table b where reason_cd <> 5..
Thats reason why i gave the LEFT JOIN to take record with matching Object id if one exists or use the OUTER APPLY to return record with same ID and max objectid if it cant find a matching objectid. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-05-08 : 15:30:42
|
The OUTER APPLY method seems a bit better on the test data, but for fun I did it without the OUTER APPLY as an alternate method:SELECT A.ID, A.object_key AS AObjectKey, COALESCE(B.object_key, C.object_key) AS BObjectKey, COALESCE(B.Reason_cd, C.Reason_cd) AS Reason_cd, COALESCE(B.labor_id, C.labor_id) AS labor_idFROM @TableA AS ALEFT OUTER JOIN @TableB AS B ON A.ID = B.ID AND A.object_key = B.object_key AND Reason_cd <> 5LEFT OUTER JOIN ( SELECT B.ID, B.object_key, B.Reason_cd, B.labor_id, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY object_key DESC) AS RowNum FROM @TableB AS B WHERE Reason_cd <> 5 ) AS C ON A.ID = C.ID AND C.RowNum = 1 |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2008-05-08 : 16:47:46
|
quote: Originally posted by Lamprey The OUTER APPLY method seems a bit better on the test data, but for fun I did it without the OUTER APPLY as an alternate method:SELECT A.ID, A.object_key AS AObjectKey, COALESCE(B.object_key, C.object_key) AS BObjectKey, COALESCE(B.Reason_cd, C.Reason_cd) AS Reason_cd, COALESCE(B.labor_id, C.labor_id) AS labor_idFROM @TableA AS ALEFT OUTER JOIN @TableB AS B ON A.ID = B.ID AND A.object_key = B.object_key AND Reason_cd <> 5LEFT OUTER JOIN ( SELECT B.ID, B.object_key, B.Reason_cd, B.labor_id, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY object_key DESC) AS RowNum FROM @TableB AS B WHERE Reason_cd <> 5 ) AS C ON A.ID = C.ID AND C.RowNum = 1
Thank you very much |
 |
|
|
|
|
|
|
|