簡體   English   中英

將包含相同ID但數據不同的兩行合並為一行

[英]Merge two rows that contain the same Id but different data into one row

我對大家有一個非常棘手的問題

假設我有下表

AccountType

Id       Type
 1     Checking
 2      Savings 

Customer_Account

Customer_Id      Account_Type_Id
    450                 1
    450                 2
    451                 1

Customer_Account包含客戶ID和其帳戶類型ID的列表。 Account_Type_Id是來自AccountType.Id的外鍵。

假設在Customer_Account表中,名為Josh(id 450)的客戶可以同時具有支票帳戶和儲蓄帳戶,如上所示。 我可以通過在AccountType表上兩次進行一次LEFT JOIN來輸出此客戶及其ID和帳戶類型:

SELECT CustAccount.Customer_Id AS Id, Account1.Type AS Account_Type_1, Account2.Type AS Account_Type_2
FROM Customer_Account CustAccount
LEFT JOIN AccountType Account1
ON Account1.Id = CustAccount.Account_Type_Id
LEFT JOIN AccountType Account2
ON Account2.Id = CustAccount.Account_Type_Id

輸出將是:

 Id        Account_Type_1         Account_Type_2           
450           Checking               Checking 
450           Savings                Savings
451           Checking               Checking   

我要嘗試做的是,如果像Josh(id 450)這樣的客戶同時擁有支票賬戶和儲蓄賬戶,我想將上述兩行數據輸出為一行,如下所示:

 Id        Account_Type_1      Account_Type_2
450           Checking            Savings

而且,如果客戶只有一種類型的帳戶(例如此處的ID為451的客戶),我只希望該類型的帳戶出現在相應的列下,如下所示:

 Id        Account_Type_1      Account_Type_2
451           Checking            

或者,如果ID為451的客戶只有一個儲蓄帳戶,則輸出應為:

Id        Account_Type_1      Account_Type_2
451                              Savings

我希望“檢查”僅顯示在Accoun_Type_1下,而“儲蓄”顯示在Account_Type_2下。 如果我執行GROUP BY CustAccount.Customer_Id,則會得到以下信息:

 Id        Account_Type_1      Account_Type_2
450           Checking            Checking
451           Checking            Checking

任何專家的幫助將不勝感激。

謝謝。

這看起來像是FULL OUTER JOIN的簡單應用程序:

SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
  FULL OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac1.Id = ac2.Id;

如果您的DBMS不支持FULL OUTER JOIN但支持LEFT OUTER JOIN,則可以使用:

SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
    ON ac0.id = ac1.id
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac0.Id = ac2.Id;

第一個子查詢生成存在的客戶ID列表; 第二個生成帳戶類型1(檢查)的列表; 第三個生成帳戶類型2(保存)的列表。 聯接可確保正確識別每個帳戶。

我認為應該對您有幫助

1Sql選擇查詢問題。 合並行或其他

應該對你有幫助

為您的ON子句添加更多條件:

ON Account1.Id = CustAccount.Account_Type_Id and Account1.Account_Type_Id = 1

ON Account2.Id = CustAccount.Account_Type_Id and Account2.Account_Type_Id = 2

將導致輸出包括客戶持有的帳戶。 如果他們只有一個帳戶,則另一帳戶類型將為NULL。

編輯:對不起,我沒有意識到您沒有客戶表。 您可以使用不同的Customer_Id值列表創建一個臨時表,並將其用作JOIN中的第一個表。

select distinct Customer_Id into #Customers
    from Customer_Account

或者更簡單:

select distinct C.Customer_Id,
    ( select 'Checking' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 1 ) as Account_Type_1,
    ( select 'Savings' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 2 ) as Account_Type_2,
    from Customer_Account as C

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM