Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Convert Cross Join to Inner Join

$
0
0
Hopefully this isn't too daunting given that I'm providing a rather lengthy piece of sample code. I understand that cross joins using where clauses are the same as inner joins but I would rather display the code given to me as inner joins. A record can have up to ten descriptions and those need to be displayed all on one line rather than as ten separate rows which is why there are joins on the same tables multiple times. I can make the obvious joins below that only involve joining the table once [code="sql"]FROM Table1 T1INNER JOIN Table2 T2 ON T1.nbr = T2.nbrINNER JOIN Table6 T6_1 ON T6_1.nbr = T1.nb2INNER JOIN Table4 T4 ON T2.StID = T4.StIDINNER JOIN Table7 T7_1 ON T7_1.ID = T6_1.IDINNER JOIN Table5 T5_1 ON T6_1.ID2 = T5_1.IDINNER JOIN Table3 T3 ON T2.Code = T3.Code[/code]My trouble comes when try to incorporate the multiple joins of Table5, Table6, and Table7. Here is the sample code in its entirety [code="sql"]SELECT T6_1.DATE ,T3.Code ,T2.Nbr ,T2.NAME ,T4.STATE ,T2.DLR ,T1.nbr2 ,T1.Type ,T5_1.[Description] ,T5_2.[Description] ,T5_3.[Description] ,T5_4.[Description] ,T5_5.[Description] ,T5_6.[Description] ,T5_7.[Description] ,T5_8.[Description] ,T5_9.[Description] ,T5_10.[Description]FROM Table1 T1 ,Table2 T2 ,Table3 T3 ,Table4 T4 ,Table5 T5_1 ,Table5 T5_2 ,Table5 T5_3 ,Table5 T5_4 ,Table5 T5_5 ,Table5 T5_6 ,Table5 T5_7 ,Table5 T5_8 ,Table5 T5_9 ,Table5 T5_10 ,Table6 T6_1 ,Table6 T6_2 ,Table6 T6_3 ,Table6 T6_4 ,Table6 T6_5 ,Table6 T6_6 ,Table6 T6_7 ,Table6 T6_8 ,Table6 T6_9 ,Table6 T6_10 ,Table7 T7_1 ,Table7 T7_2 ,Table7 T7_3 ,Table7 T7_4 ,Table7 T7_5 ,Table7 T7_6 ,Table7 T7_7 ,Table7 T7_8 ,Table7 T7_9 ,Table7 T7_10WHERE T1.nbr = T2.nbr AND T6_1.nbr = T1.nbr2 AND T2.StID = T4.StID AND T7_1.ID = T6_1.ID AND T7_2.ID = T6_2.ID AND T7_3.ID = T6_3.ID AND T7_4.ID = T6_4.ID AND T7_5.ID = T6_5.ID AND T7_6.ID = T6_6.ID AND T7_7.ID = T6_7.ID AND T7_8.ID = T6_8.ID AND T7_9.ID = T6_9.ID AND T7_10.ID = T6_10.ID AND T6_1.ID2 = T5_1.ID AND T6_2.ID2 = T5_2.ID AND T6_3.ID2 = T5_3.ID AND T6_4.ID2 = T5_4.ID AND T6_5.ID2 = T5_5.ID AND T6_6.ID2 = T5_6.ID AND T6_7.ID2 = T5_7.ID AND T6_8.ID2 = T5_8.ID AND T6_9.ID2 = T5_9.ID AND T6_10.ID2 = T5_10.ID AND T2.Code = T3.Code[/code]Is there any easy way to convert this to inner joins or possibly some other way to be able to display the description up to ten times on the same line without having to join the tables on themselves ten times?

Viewing all articles
Browse latest Browse all 3145

Trending Articles