SQL-如何将2个表与相同列连接并水平堆叠

时间:2018-01-10 07:12:31

标签: sql sql-server-2008

我有2个表格,例如:

第一张表:

November

Account    |    Name     |   Segment    |   Collectibility  |  Loan Amount   | AFDA
1             Billy        Corporate           1                   5000         200
2             Randy        Corporate           2                   8000         1000
3             Stan         Commercial          3                   2000         200

第二桌

December

Account    |    Name     |   Segment    |   Collectibility  |  Loan Amount   | AFDA
1             Billy        Corporate           2                   2000         100
2             Randy        Corporate           3                   4000         500
4             Kenny         Small              1                   2000         100

我正在尝试加入表格,因此输出将是这样的

Nov_Dec

Account    |    Name     |   Segment_Nov    |   Segment_Dec     |   Collectibility_Nov  |   Collectibility_Dec  |  Loan Amount_Nov  |   Loan Amount_Dec   | AFDA_Nov    | AFDA_DEC
1               Billy           Corporate       Corporate               1                       2                   5000                    2000            200             100
2               Randy           Corporate       Corporate               2                       3                   8000                    4000            1000            500
3               Stan            Commercial      NULL                    3                       NULL                2000                    NULL            200             NULL
4               Kenny           NULL            Small                   NULL                    1                   NULL                    2000            NULL            100

通常,我迈出了很长的一步来实现这一目标

  1. 加入“帐户”栏目
  2. 修改设计,添加新列
  3. 根据“ACCOUNT”列加入表格,该列已合并,没有任何重复
  4. 有没有像使用子查询来固定流程的解决方法? 谢谢

2 个答案:

答案 0 :(得分:1)

您可以先UNION来自不同表格的结果,然后执行条件汇总来旋转每个月的数据:

SELECT
    Account,
    Name,
    MAX(CASE WHEN m = 'Nov' THEN Segment END) AS Segment_Nov,
    MAX(CASE WHEN m = 'Dec' THEN Segment END) AS Segment_Dec,
    MAX(CASE WHEN m = 'Dec' THEN Collectibility END) AS Collectibility_Nov,
    MAX(CASE WHEN m = 'Dec' THEN Collectibility END) AS Collectibility_Dec,
    MAX(CASE WHEN m = 'Dec' THEN LoanAmount END) AS LoanAmount_Nov,
    MAX(CASE WHEN m = 'Dec' THEN LoanAmount END) AS LoanAmount_Dec,
    MAX(CASE WHEN m = 'Dec' THEN AFDA END) AS AFDA_Nov,
    MAX(CASE WHEN m = 'Dec' THEN AFDA END) AS AFDA_Dec
FROM
(
    SELECT Account, Name, Segment, Collectibility, LoanAmount, AFDA, 'Nov' AS m
    FROM Table1

    UNION ALL

    SELECT Account, Name, Segment, Collectibility, LoanAmount, AFDA, 'Dec' AS m
    FROM Table2
) AS t
GROUP BY
    Account,
    Name;

答案 1 :(得分:0)

按照这种方式,您将得到结果:

    DECLARE @Table1 TABLE(Account INT,Name VARCHAR(10),Segment VARCHAR(10),Collectibility INT,LoanAmount INT,AFDA INT)
    INSERT INTO @Table1 VALUES(1,'Billy','Corporate',1,5000,200)
    INSERT INTO @Table1 VALUES(2,'Randy','Corporate',2,8000,1000)
    INSERT INTO @Table1 VALUES(3,'Stan','Commercial',3,2000,200)

    DECLARE @Table2 TABLE(Account INT,Name VARCHAR(10),Segment VARCHAR(10),Collectibility INT,LoanAmount INT,AFDA INT)
    INSERT INTO @Table2 VALUES(1,'Billy','Corporate',2,2000,100)
    INSERT INTO @Table2 VALUES(2,'Randy','Corporate',3,4000,500)
    INSERT INTO @Table2 VALUES(4,'Kenny','Small',1,2000,100)

    SELECT COALESCE(T1.Account,T2.Account)Account,COALESCE(T1.Name,T2.Name)Name
        ,MAX(T1.Segment)Segment_Nov,MAX(T2.Segment)Segment_Dec
        ,MAX(T1.Collectibility)Collectibility_Nov,MAX(T2.Collectibility)Collectibility_Dec
        ,MAX(T1.LoanAmount)LoanAmount_Nov,MAX(T2.LoanAmount)LoanAmount_Dec
        ,MAX(T1.AFDA)AFDA_Nov,MAX(T2.AFDA)AFDA_Dec
    FROM @Table1 T1
    FULL JOIN @Table2 T2 ON T1.Account=T2.Account
    GROUP BY COALESCE(T1.Account,T2.Account),COALESCE(T1.Name,T2.Name)
    ORDER BY COALESCE(T1.Account,T2.Account)

<强>结果:

Account     Name       Segment_Nov Segment_Dec Collectibility_Nov Collectibility_Dec LoanAmount_Nov LoanAmount_Dec AFDA_Nov    AFDA_Dec
----------- ---------- ----------- ----------- ------------------ ------------------ -------------- -------------- ----------- -----------
1           Billy      Corporate   Corporate   1                  2                  5000           2000           200         100
2           Randy      Corporate   Corporate   2                  3                  8000           4000           1000        500
3           Stan       Commercial  NULL        3                  NULL               2000           NULL           200         NULL
4           Kenny      NULL        Small       NULL               1                  NULL           2000           NULL        100