如何在 SQL Server 中将行合并为一行

发布于 2024-11-18 23:25:44 字数 857 浏览 3 评论 0原文

我有一个这样的表:

 - 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

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

I have a table like this:

 - 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

There are totally 4 currencies 1,2,3,4

There are totally 3 lend 1,2,3

I want to get a result like below:

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

I know it is now good description, but what I want to do is to merge many records to one record.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-11-25 23:25:44

这称为透视,进行透视的方法之一是使用带条件聚合的分组:

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。

This is called pivoting and one of the ways how to do the pivoting is to use grouping with conditional aggregating:

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

If all the (CurrencyID, LendID) combinations are guaranteed to be unique within same ID groups, you can also use MIN or MAX instead of SUM.

岁月苍老的讽刺 2024-11-25 23:25:44

您可以使用从 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

注意:我保留了你的专栏名称

You could use the PIVOT Syntax starting with 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

which outputs

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

Note: I kept your column names

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文