简体   繁体   English

将行数据转换并分组为列

[英]Transform and group row data into columns

I'm looking for a way to combine different rows into a single row with columns. 我正在寻找一种将不同的行合并为带有列的单行的方法。

Let's take this example table: 让我们来看这个示例表:

destinationIP       sourceDataCenter         latency
-------------       ----------------         -------
1.1.1.1             Data Center A            10ms
1.1.1.1             Data Center B            12ms
1.1.1.1             Data Center C            5ms
1.1.1.2             Data Center A            50ms

Desired output: 所需的输出:

destinationIP     Data Center A     Data Center B     Data Center C
-------------     -------------     -------------     -------------     
1.1.1.1           10ms              12ms              5ms
1.1.1.2           50ms

Please note that Data centers are not necessarily these three, they can be N different data centers that I will not know in advance. 请注意,数据中心不一定是这三个,它们可以是我事先不知道的N不同的数据中心。

select   destinationIP
,        max(case when sourceDataCenter = 'Data Center A' then latency end) as A
,        max(case when sourceDataCenter = 'Data Center B' then latency end) as B
,        max(case when sourceDataCenter = 'Data Center C' then latency end) as C
from     YourTable
group by
         destinationIP

With a known set of sourceDataCenter values, you can just use a simple PIVOT operator : 有了一组已知的sourceDataCenter值,您就可以使用一个简单的PIVOT运算符

DECLARE @x TABLE
(
  destinationIP    VARCHAR(15), 
  sourceDataCenter VARCHAR(255), 
  latency          VARCHAR(32)
);

INSERT @x VALUES
('1.1.1.1','Data Center A','10ms'),
('1.1.1.1','Data Center B','12ms'),
('1.1.1.1','Data Center C','5ms'),
('1.1.1.2','Data Center A','50ms');

SELECT destinationIP, [Data Center A], [Data Center B], [Data Center C]
FROM @x AS x 
PIVOT 
(
  MAX(latency) FOR sourceDataCenter IN 
  (
    [Data Center A],[Data Center B],[Data Center C]
  )
) AS p
ORDER BY destinationIP;

If you don't know the names of the data centers in advance, you need to use dynamic SQL to generate an equivalent query, first by getting the distinct list of values, and then adding those into the two relevant spots in the query: 如果您事先不知道数据中心的名称,则需要使用动态SQL来生成等效的查询,首先获取不同的值列表,然后将它们添加到查询中的两个相关位置:

USE tempdb;
GO

CREATE TABLE dbo.YourTable
(
  destinationIP    VARCHAR(15), 
  sourceDataCenter VARCHAR(255), 
  latency          VARCHAR(32)
);

INSERT dbo.YourTable VALUES
('1.1.1.1','Data Center A','10ms'),
('1.1.1.1','Data Center B','12ms'),
('1.1.1.1','Data Center C','5ms'),
('1.1.1.2','Data Center A','50ms');

DECLARE @cols NVARCHAR(MAX) = N'', @sql NVARCHAR(MAX);

SELECT @cols = (SELECT ',' + QUOTENAME(sourceDataCenter)
  FROM dbo.YourTable GROUP BY sourceDataCenter ORDER BY sourceDataCenter
  FOR XML PATH(''), TYPE).value('.[1]','nvarchar(max)');

SELECT @sql = N'SELECT destinationIP' + @cols + '
 FROM dbo.YourTable AS x 
 PIVOT 
 (
   MAX(latency) FOR sourceDataCenter IN (' + STUFF(@cols,1,1,'') + ')
 ) AS p
 ORDER BY destinationIP;';

EXEC sp_executesql @sql;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM