SQL Server加入不等数量的行

时间:2015-05-11 04:40:45

标签: sql-server

我有如下数据,并希望看到如下结果。 结果中应包含空格。表2或表3可能包含的行数多于另一个,并且结果中应包含空格/或空值。

表1

C1=29 (pk)  C2=4133     
C1=33 (pk)  C2=9375     

表2

C1=29(fk)   C2=Adam     
C1=29(fk)   C2=Bob      
C1=29(fk)   C2=Chris        
C1=29(fk)   C2=Dave     
C1=33(fk)   C2=Eddie        
C1=33(fk)   C2=Frank        

表3

C1=29(fk)   C2=Helen        
C1=29(fk)   C2=Joice        
C1=33(fk)   C2=Karen        
C1=33(fk)   C2=Laura        
C1=33(fk)   C2=Mary     

期望的结果

1.C1=29 1.C2=4133   2.C2=Adam   3.C2=Helen
1.C1=29 1.C2=4133   2.C2=Bob    3.C2=Joice
1.C1=29 1.C2=4133   2.C2=Chris  
1.C1=29 1.C2=4133   2.C2=Dave   
1.C1=33 1.C2=9375   2.C2=Eddie  3.C2=Karen
1.C1=33 1.C2=9375   2.C2=Frank  3.C2=Laura
1.C1=33 1.C2=9375               3.C2=Mary

Rohit-s查询给出 -

SELECT t1.col1, t1.col2, t2.col2, t3.col2
FROM dbo.Table1 t1
LEFT JOIN dbo.Table2 t2 ON t1.col1 = t2.col1
LEFT JOIN dbo.Table3 t3 ON t1.col1 = t3.col1

col1    col2    col2    col2
29  4133    Adam    Helen
29  4133    Adam    Joice
29  4133    Bob Helen
29  4133    Bob Joice
29  4133    Chris   Helen
29  4133    Chris   Joice
29  4133    Dave    Helen
29  4133    Dave    Joice
33  9375    Eddie   Karen
33  9375    Eddie   Laura
33  9375    Eddie   Mary
33  9375    Frank   Karen
33  9375    Frank   Laura
33  9375    Frank   Mary

SQL FIDDLE LINK

1 个答案:

答案 0 :(得分:1)

你只能LEFT JOIN所有的桌子吗?

例如:

SELECT t1.C1, t1.C2,t2.C2, t3.C2
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.C1 = t2.C1
LEFT JOIN Table3 t3 on t1.C1 = t3.C1

编辑: 根据您的编辑,并希望订购的内容:

SELECT t1.C1, t1.C2, t2.C2, t3.C2
FROM Table1 t1
LEFT JOIN (
             SELECT C1, C2, row_number() OVER (ORDER BY C1, C2) AS row_num
             FROM Table2
          ) t2 ON t1.C1 = t2.C1
LEFT JOIN (
             SELECT C1, C2, row_number() OVER (ORDER BY C1, C2) AS row_num
             FROM Table3
          ) t3 ON t1.C1 = t3.C1 AND t3.row_num = t2.row_num

编辑2: 假设一切正常,(Row_Number和Dense_Rank):

SELECT t1.C1, t1.C2, t2.C2, t3.C2
FROM Table1 t1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table2
    ) t2 ON t1.C1 = t2.C1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table3
    ) t3 ON t1.C1 = t3.C1 AND t3.group_num = t2.group_num AND t3.row_num = t2.row_num

编辑3: Dense_Rank,Row_Number,Union

SELECT t1.C1 AS T1C1, t1.C2 AS T1C2, t2.C2 AS T2C2, t3.C2 AS T3C3
FROM Table1 t1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table2
    ) t2 ON t1.C1 = t2.C1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table3
    ) t3 ON t1.C1 = t3.C1 AND t3.group_num = t2.group_num AND t3.row_num = t2.row_num

UNION

SELECT t1.C1, t1.C2, t2.C2, t3.C2
FROM Table1 t1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table3
    ) t3 ON t1.C1 = t3.C1
LEFT JOIN (
    SELECT C1, C2, DENSE_RANK() OVER (
            ORDER BY C1
            ) AS group_num, row_number() OVER (
            PARTITION BY C1 ORDER BY C1, C2
            ) AS row_num
    FROM Table2
    ) t2 ON t1.C1 = t2.C1 AND t3.group_num = t2.group_num AND t3.row_num = t2.row_num
ORDER BY T1C1, T1C2, T2C2, T3C3

编辑4: Dense_Rank,Row_Number,Union,SubQuery,Order By

SELECT *
FROM (
        SELECT t1.Col1 AS T1C1, t1.Col2 AS T1C2, t2.Col2 AS T2C2, t3.Col2 AS T3C3
        FROM Table1 t1
        LEFT JOIN (
            SELECT Col1, Col2, DENSE_RANK() OVER (
                    ORDER BY Col1
                    ) AS group_num, row_number() OVER (
                    PARTITION BY Col1 ORDER BY Col1, Col2
                    ) AS row_num
            FROM Table2
            ) t2 ON t1.Col1 = t2.Col1
        LEFT JOIN (
            SELECT Col1, Col2, DENSE_RANK() OVER (
                    ORDER BY Col1
                    ) AS group_num, row_number() OVER (
                    PARTITION BY Col1 ORDER BY Col1, Col2
                    ) AS row_num
            FROM Table3
            ) t3 ON t1.Col1 = t3.Col1 
                 AND t3.group_num = t2.group_num 
                 AND t3.row_num = t2.row_num

        UNION

        SELECT t1.Col1, t1.Col2, t2.Col2, t3.Col2
        FROM Table1 t1
        LEFT JOIN (
            SELECT Col1, Col2, DENSE_RANK() OVER (
                    ORDER BY Col1
                    ) AS group_num, row_number() OVER (
                    PARTITION BY Col1 ORDER BY Col1, Col2
                    ) AS row_num
            FROM Table3
            ) t3 ON t1.Col1 = t3.Col1
        LEFT JOIN (
            SELECT Col1, Col2, DENSE_RANK() OVER (
                    ORDER BY Col1
                    ) AS group_num, row_number() OVER (
                    PARTITION BY Col1 ORDER BY Col1, Col2
                    ) AS row_num
            FROM Table2
            ) t2 ON t1.Col1 = t2.Col1 
                 AND t3.group_num = t2.group_num 
                 AND t3.row_num = t2.row_num
        ) AS SUB
ORDER BY T1C1, T1C2
, CASE WHEN T2C2 IS NULL THEN 1 ELSE 0 END
, CASE WHEN T3C3 IS NULL THEN 1 ELSE 0 END

SQL Fiddle Link