sqlserver记录需要来到horizo​​ntly

时间:2017-10-13 11:20:58

标签: sql sql-server

下面有表结构,

表名:客户

Cust_id    Cust_Name
------------------------   
    101        John
    102        Peter

表名:Loan_Details

    Id   LoanName         Cust_Id    Amt
  -----------------------------------------
    1    PersonalLoan    101        2L 
    2    PersonalLoan    101        3L
    3    HomeLoan        101        10L

表名:产品

    Id    Cust_Id     ProductName    Cost
   -----------------------------------------
    1     101        Product1        1000 
    2     101        Product1        2000
    3     101        Product1        3000

我需要在下面的结构中进行水平结果,是否可能?

Cust_Id Cust_Name   PersonalLoan    Amt   HomeLoan   Amt    ProductName   Cost
--------------------------------------------------------------------------------
101     John        PersonalLoan    2L    HomeLoan   10L    Product1       1000
101     John        PersonalLoan    3L    NULL       NULL   Product1       2000
101     John        NULL            NULL  NULL       NULL   Product1       3000

2 个答案:

答案 0 :(得分:1)

您可以尝试此查询。

如果你想列出&#34; Peter&#34;也可以在CusPrd子查询中将<span>更改为INNER JOIN

LEFT JOIN

结果:

DECLARE @Customer TABLE (Cust_id VARCHAR(10),    Cust_Name VARCHAR(20))
INSERT INTO @Customer
VALUES 
('101','John'),
('102','Peter')

DECLARE @Loan_Details TABLE  (Id INT, LoanName VARCHAR(20), Cust_Id VARCHAR(10),    Amt VARCHAR(10))
INSERT INTO @Loan_Details 
VALUES
(1,'PersonalLoan','101','2L'), 
(2,'PersonalLoan','101','3L'),
(3,'HomeLoan','101','10L')

DECLARE @Product TABLE (Id INT, Cust_Id VARCHAR(10), ProductName VARCHAR(20),   Cost INT)
INSERT INTO @Product
VALUES
(1,'101','Product1',1000),
(2,'101','Product1',2000),
(3,'101','Product1',3000)


;WITH Loan AS 
(
    SELECT *, 
        RN = ROW_NUMBER() OVER (PARTITION BY LoanName ORDER BY Id) 
    FROM @Loan_Details
),
CusPrd AS 
(
    SELECT 
        C.Cust_id, 
        C.Cust_Name,
        P.ProductName, 
        P.Cost, 
        P.Id, 
        RN = ROW_NUMBER() OVER (PARTITION BY C.Cust_id ORDER BY P.Id)
    FROM 
        @Customer C 
        INNER JOIN  @Product P ON C.Cust_id = P.Cust_Id
)

SELECT  C.Cust_id, C.Cust_Name, PL.LoanName [PersonalLoan], PL.Amt, HL.LoanName [HomeLoan], HL.Amt, C.ProductName, C.Cost 
FROM 
    CusPrd C
    LEFT JOIN Loan PL ON PL.Cust_Id = C.Cust_id AND PL.LoanName ='PersonalLoan' AND C.RN = PL.RN
    LEFT JOIN Loan HL ON HL.Cust_Id = C.Cust_id AND HL.LoanName ='HomeLoan' AND PL.RN = HL.RN AND C.RN = PL.RN

答案 1 :(得分:-1)

select * from
(select c.Cust_id,Cust_Name,ld.LoanName as Loan,Amt,ProductName,Cost
from Customer c left join Product p on p.Cust_id=c.Cust_id
left join Loan_Details ld on ld.Cust_id = c.Cust_id) as MTable
PIVOT
(
SUM(Amt) for Loan in ([HomeLoan],[PersonalLoan])
) as PTable

使用它,它会给你你想要的结果是这样的

Cust_id     Cust_Name            ProductName          Cost        HomeLoan    PersonalLoan
----------- -------------------- -------------------- ----------- ----------- ------------
101         John                 Product1             1000        10          5
101         John                 Product12            2000        10          5
101         John                 Product2             3000        10          5
102         Peter                NULL                 NULL        NULL        NULL