【问题标题】:How to merge rows to one row in SQL Server如何在 SQL Server 中将行合并为一行
【发布时间】:2011-09-30 03:02:39
【问题描述】:

我有一张这样的桌子:

 - 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

我知道现在描述得很好,但我想做的是将多条记录合并为一条记录。

【问题讨论】:

  • 嗨。欢迎来到 SO。请正确格式化您的问题。
  • 要格式化代码或数据样本,您可以编辑您的问题,选择您的代码并按 {} 按钮。我会这样做,但我在手机上
  • 我格式化了表格,但我完全不知道输出的实际意图是什么。

标签: sql-server merge rows


【解决方案1】:

这称为pivoting,其中一种方法是使用带有条件聚合的分组:

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。

【讨论】:

  • 几个小问题 当 OP 想要零时,这会输出 null,因此您必须添加一个 COALESCE。也仅适用于 LendID CurrentID = 1 and LendID = 11 然后 CurrencyIDLendID = 21 与 CurrentID = 2 and LendID =1 相同的结果可能不正确。
  • @Conrad:您说的完全正确,当然需要 COALESCE。我也同意另一点,但是关于货币和贷款的数量的问题非常明确,所以我认为乘以 10 与乘以 100 或任何其他 10 的幂一样好。基本概念应该很清楚无论如何。
【解决方案2】:

您可以使用从 SQL Server 2005 开始的 PIVOT 语法

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

注意:我保留了您的列名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2018-04-22
    • 2017-09-24
    • 2016-07-12
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多