2011-07-07 55 views
0

我有這樣的一個表:如何行合併一行在SQL Server

- ID | CurrencyID | LendID | Price 
- 3 | 1  | 1  | 1.2 
- 3 | 1  | 2  | 1.3 
- 3 | 1  | 3  | 1.4 
- 3 | 2  | 1  | 1.5 
- 3 | 2  | 2  | 1.6 
- 3 | 2  | 3  | 1.7 
- 4 | 2  | 3  | 2.0 

,總共有4貨幣1,2,3,4

,總共有3借給1,2,3

我想得到如下結果:

ID | CurrencyIDLendID_11_Price | CIDID_12_Price | CIDLID_13_Price | CIDLID_21_Price | CIDLID_22_Price | CIDLID_23_Price | CIDLID_31_Price | CIDLID_32_Price | CIDLID_33_Price | CIDLID_41_Price | CIDLID_42_Price | CIDLID_43_Price 
3 | 1.2 | 1.3 | 1.4 | 1.5 | 1.6 | 1.7 | 0 | 0 | 0 | 0 | 0 | 0 
4 | 0 | 0 | 0 | 0 | 0 | 2.0 | 0 | 0 | 0 | 0 | 0 | 0 

我知道這是現在很好descrip重刑,但我想要做的是合併很多記錄到一個記錄。

+1

嗨。歡迎來到SO。請正確格式化您的問題。 – bernie

+1

要格式化代碼或數據樣本,您可以編輯您的問題,選擇您的代碼並按{}按鈕。我會這樣做,但我在我的手機上 –

+0

我格式化了表格,但我完全不知道輸出結果實際上是什麼樣子。 –

回答

1

這就是所謂的旋轉和的方式之一是如何做的轉動是使用具有有條件的聚集分組:

WITH cidlid AS (
    SELECT 
    ID, 
    CurrencyIDLendID = CurrencyID * 10 + LendID, 
    Price 
    FROM atable 
) 
SELECT 
    ID, 
    CurrencyIDLendID_11_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 11 THEN Price END), 0), 
    CurrencyIDLendID_12_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 12 THEN Price END), 0), 
    CurrencyIDLendID_13_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 13 THEN Price END), 0), 
    CurrencyIDLendID_21_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 21 THEN Price END), 0), 
    CurrencyIDLendID_22_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 22 THEN Price END), 0), 
    CurrencyIDLendID_23_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 23 THEN Price END), 0), 
    CurrencyIDLendID_31_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 31 THEN Price END), 0), 
    CurrencyIDLendID_32_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 32 THEN Price END), 0), 
    CurrencyIDLendID_33_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 33 THEN Price END), 0), 
    CurrencyIDLendID_41_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 41 THEN Price END), 0), 
    CurrencyIDLendID_42_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 42 THEN Price END), 0), 
    CurrencyIDLendID_43_Price = COALESCE(SUM(CASE CurrencyIDLendID WHEN 43 THEN Price END), 0) 
FROM cidlid 
GROUP BY ID 

如果所有的(CurrencyID, LendID)組合,保證是同一個ID組中是唯一的,你也可以使用MIN或MAX來代替SUM。

+0

一些小問題當OP要求零時,這個輸出爲null,所以你必須添加一個COALESCE。例如,如果'CurrentID = 1且LendID = 11',則'CurrencyIDLendID = 21'與'CurrentID = 2和LendID = 1'相同,可能不正確。 –

+0

@康拉德:你說的很對,當然需要聯盟。我也同意另外一點,但問題是關於貨幣和借貸的數量是非常明確的,所以我認爲乘以10將會乘以100或10的任何其他權力。基本思想應該清楚無論如何。 –

1

您可以使用PIVOT語法開始與SQL Server 2005

Declare @Data table (ID int, CurrencyID int, LendID int , price decimal (4,2)) 

INSERT INTO @Data 

SELECT 3 as ID, 1 as CurrencyID, 1 as LendID , 1.2 as price 
UNION SELECT 3 , 1  , 1  , 1.2 
UNION SELECT 3 , 1  , 2  , 1.3 
UNION SELECT 3 , 1  , 3  , 1.4 
UNION SELECT 3 , 2  , 1  , 1.5 
UNION SELECT 3 , 2  , 2  , 1.6 
UNION SELECT 3 , 2  , 3  , 1.7 
UNION SELECT 4 , 2  , 3  , 2.0 



SELECT 
    ID, 
    COALESCE([1_1],0) as CurrencyIDLendID_11_Price , 
    COALESCE([1_2],0) as CIDID_12_Price , 
    COALESCE([1_3],0) as CIDLID_13_Price , 
    COALESCE([2_1],0) as CIDLID_21_Price , 
    COALESCE([2_2],0) as CIDLID_22_Price , 
    COALESCE([2_3],0) as CIDLID_23_Price , 
    COALESCE([3_1],0) as CIDLID_31_Price , 
    COALESCE([3_2],0) as CIDLID_32_Price , 
    COALESCE([3_3],0) as CIDLID_33_Price , 
    COALESCE([4_1],0) as CIDLID_41_Price , 
    COALESCE([4_2],0) as CIDLID_42_Price , 
    COALESCE([4_3],0) as CIDLID_43_Price 
FROM (
SELECT 
     ID, 
     cast(CurrencyID as varchar) + '_' + CAST(lendID as varchar) ColumnHeader, 
     price FROM @Data) src 
PIVOT (SUM(price) for ColumnHeader IN 
     ([1_1], [1_2],[1_3], 
      [2_1], [2_2],[2_3], 
      [3_1], [3_2],[3_3], 
      [4_1], [4_2],[4_3]) 


) as pivottable 

其輸出

ID   CurrencyIDLendID_11_Price    CIDID_12_Price       CIDLID_13_Price       CIDLID_21_Price       CIDLID_22_Price       CIDLID_23_Price       CIDLID_31_Price       CIDLID_32_Price       CIDLID_33_Price       CIDLID_41_Price       CIDLID_42_Price       CIDLID_43_Price 
----------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- 
3   1.20         1.30         1.40         1.50         1.60         1.70         0.00         0.00         0.00         0.00         0.00         0.00 
4   0.00         0.00         0.00         0.00         0.00         2.00         0.00         0.00         0.00         0.00         0.00         0.00 

注:我一直列名