2011-09-04 40 views
-1

我有一個非常棘手的問題要問你所有合併含有相同ID但不同的數據轉換成一排兩排

假設我有以下各表

ACCOUNTTYPE

Id  Type 
1  Checking 
2  Savings 

Customer_Account

Customer_Id  Account_Type_Id 
    450     1 
    450     2 
    451     1 

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

假設在Customer_Account表,命名爲約什(ID 450)一個客戶可以有一個檢查和上面示出的儲蓄賬戶作爲。我可以輸出這個客戶與他的ID和像這樣的帳戶類型由具有LEFT JOIN兩次在ACCOUNTTYPE表:

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 

任何專家的任何幫助將是非常讚賞。

謝謝。

+0

我不明白你的表是什麼樣的。表'Customers'有一個'Account_Id'字段和'Customer_Id'字段?而'Josh'在這張表中出現了兩次,兩個都有相同的Customer_Id? –

+0

表客戶有列Id,Name,Account_Type_1,Account_Type_2和Account_Type_Id(來自表AccountType的外鍵) – user765368

+0

我很確定你在這裏感到困惑。 Account_Type_1是您在查詢的結果集中引入的名稱,它似乎不是您的Customers表中的列。 –

回答

2

這看起來像一個完整的OUTER一個簡單的應用程序連接:

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(保存)的列表。這些連接可確保每個帳戶都被正確識別。

+0

WOW !!!!!這工作!我無法相信這需要如此複雜的查詢來做一些聽起來像這樣簡單的事情。非常感謝你!!! – user765368

1

我覺得this應該幫助你

1Sql Select Query Question. Merge Rows否則

this應該幫助你

+0

至於第一個鏈接,這不是我的同樣的問題。在他們的問題中,他們的表中只有一列有不同的值。我,我有兩列交替不同的值,這是不同的。我想要的實際上是GROUP BY類型的東西,將這兩行合併到一個表中的單個行中。至於聯盟,我相信它只適用於兩條SELECT語句之間。我只用一條SELECT語句就不能做到這一點嗎? – user765368

1

增加更多的條件,您的從句:

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

AccountType表中沒有Account_Type_Id列,但我明白你的意思。它不起作用,因爲它仍然顯示兩次,而且現在它爲第二個帳戶輸出NULL類型 – user765368

+0

從Customer_Account中選擇'正在檢查'? – user765368

+0

您可以使用查找來從id(1)獲取帳戶類型(「Checking」),或者只是選擇一個常量。相關子查詢的結果將是常量值或NULL。可以使用COALESCE或CASE來提供默認值,例如, 「N/A」。嘗試玩:SELECT 42 AS ANSWER,(SELECT'ME'where 1 = 2)AS CHOSEN – HABO

相關問題