【问题标题】:SQL Server : merge multiple rows into 1 rowSQL Server:将多行合并为 1 行
【发布时间】:2018-10-05 09:14:50
【问题描述】:

我使用的是 SQL Server 2012 Standard,并且与将多行合并为一个的性能相关。

例子:

我可以使用下面的查询来按预期获取数据,但性能不佳。 还有其他性能更好的查询吗?

WITH Data AS
(
    SELECT 88 ID, 1 AS [OrderFW],'a' AS Code,'n1' as Name UNION
    SELECT 88 ID,2 AS [OrderFW],'a' AS Code,'n2' as Name UNION
    SELECT 88 ID,3 AS [OrderFW],'a' AS Code,'n3' as Name UNION
    SELECT 99 ID,1 AS [OrderFW],'b' AS Code,'n4' as Name UNION
    SELECT 99 ID,2 AS [OrderFW],'b' AS Code,'n5' as Name
)
SELECT 
    d1.Code code1, d1.Name name1, 
    d2.Code code2, d2.Name name2,
    d3.Code code3, d3.Name name3
FROM
    Data d1
LEFT OUTER JOIN
    Data d2 ON d1.ID = d2.ID AND d2.OrderFW = 2
LEFT OUTER JOIN
    Data d3 ON d1.ID = d3.ID AND d3.OrderFW = 3
WHERE
    d1.OrderFW = 1

【问题讨论】:

  • 请注意,我在 View 中使用查询,所以我们无法创建临时表。仅查询
  • @YogeshSharma 为此提供了一个很好的解决方案。除了将查询更改为他建议的查询之外,您还有哪些索引该视图/基础表?为获得新查询的最佳性能,请创建包含列IdOrderFW 的索引。从您的示例数据中,我猜这可能是一个独特的列;但你必须打那个电话……

标签: sql sql-server tsql query-performance


【解决方案1】:

我会尝试聚合:

select Id, 
       max(case when seq = 1 then code end) as code1,
       max(case when seq = 1 then name end) as name1,
       max(case when seq = 2 then code end) as code2,
       max(case when seq = 2 then name end) as name2,
       max(case when seq = 3 then code end) as code3,
       max(case when seq = 3 then name end) as name3
from (select d.*,
             row_number() over (partition by code order by order) as seq
      from data d
     ) d
group by Id;

编辑:如果您已经有sequence(即Order),那么只有聚合就足够了:

select Id, 
       max(case when [order] = 1 then code end) as code1,
       max(case when [order] = 1 then name end) as name1,
       max(case when [order] = 2 then code end) as code2,
       max(case when [order] = 2 then name end) as name2,
       max(case when [order] = 3 then code end) as code3,
       max(case when [order] = 3 then name end) as name3
from data d
group by Id;

编辑:感谢JohnLBevan 的演示。

这里是SQL Fiddle Example

【讨论】:

  • 注意:在示例中,OrderFW 字段可用于此目的,因此您无需为此目的生成新列(即您可以删除 row_number() over (partition by code order by order) as seq)。跨度>
  • @JohnLBevan。 . .感谢小提琴演示。
猜你喜欢
  • 2011-12-21
  • 2023-01-30
  • 2012-05-27
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
相关资源
最近更新 更多