简体   繁体   中英

sqlserver records need to come horizontly

I have table structure below,

Table Name: Customer

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

Table Name: Loan_Details

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

Table Name: Product

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

I need result horizontly in below structure, is it possble?

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

You can try this query.

If you want to list "Peter" too, just change INNER JOIN to LEFT JOIN in the CusPrd subquery.

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

Result:

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
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

Use this it will give you what you want the result is something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM