一尘不染

在SQL Server中动态将多行合并为多列

sql

我有一个很大的数据库表,需要在上面使用Microsoft SQL Server动态执行以下操作。

从这样的结果:

 badge   |   name   |   Job   |   KDA   |   Match 
 - - - - - - - - - - - - - - - -
 T996    |  Darrien |   AP    |   3.0   |   20
 T996    |  Darrien |   ADC   |   2.8   |   16
 T996    |  Darrien |   TOP   |   5.0   |   120

使用SQL的结果如下:

badge   |   name   |  AP_KDA | AP_Match | ADC_KDA | ADC_Match | TOP_KDA | TOP_Match 
- - - - - - - - -
T996    |  Darrien |   3.0   |   20     |  2.8    |   16      |   5.0   |  120

即使有30行,它也将合并为具有60列的单行。

我目前可以通过硬编码(请参见下面的示例)来做到这一点,但是不能动态地做到。

Select badge,name,
(
 SELECT max(KDA)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'AP')
) AP_KDA,
(
 SELECT max(Match)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'AP')
) AP_Match,
(
 SELECT max(KDA)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'ADC')
) ADC_KDA,
(
 SELECT max(Match)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'ADC')
) ADC_Match,
(
 SELECT max(KDA)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'TOP')
) TOP_KDA,
(
 SELECT max(Match)
 FROM table
 WHERE (h.badge = badge) AND (h.name = name) 
 AND (Job = 'TOP')
) TOP_Match
from table h

我需要一个MSSQL语句,该语句允许我将多行合并为一行。第3列的Job内容将与第4列和第5列的标头(KDAMatch)合并,成为新的列。

所以,如果有6倍不同的值Job(比如Job1通过Job6),那么结果将有12列,如:Job1_KDAJob1_MatchJob2_KDAJob2_Match,等,由徽章和名称分组。

我需要一个可以遍历第3列数据的语句,因此不需要硬编码(针对每个可能的Job值重复查询)或使用临时表。


阅读 219

收藏
2021-03-17

共1个答案

一尘不染

我会使用动态SQL来做到这一点,但这是(http://sqlfiddle.com/#!6/a63a6/1/0)PIVOT解决方案:

SELECT badge, name, [AP_KDa], [AP_Match], [ADC_KDA],[ADC_Match],[TOP_KDA],[TOP_Match] FROM
(
SELECT badge, name, col, val FROM(
 SELECT *, Job+'_KDA' as Col, KDA as Val FROM @T 
 UNION
 SELECT *, Job+'_Match' as Col,Match as Val  FROM @T
) t
) tt
PIVOT ( max(val) for Col in ([AP_KDa], [AP_Match], [ADC_KDA],[ADC_Match],[TOP_KDA],[TOP_Match]) ) AS pvt

奖励:这种如何将PIVOT与动态SQL(http://sqlfiddle.com/#!6/a63a6/7/0)结合使用的方法,我还是希望在没有PIVOT的情况下更简单地进行,但这对于我

SELECT badge, name, cast(Job+'_KDA' as nvarchar(128)) as Col, KDA as Val INTO #Temp1 FROM Temp 
INSERT INTO #Temp1 SELECT badge, name, Job+'_Match' as Col, Match as Val FROM Temp

DECLARE @columns nvarchar(max)
SELECT @columns = COALESCE(@columns + ', ', '') + Col FROM #Temp1 GROUP BY Col

DECLARE @sql nvarchar(max) = 'SELECT badge, name, '+@columns+' FROM #Temp1 PIVOT ( max(val) for Col in ('+@columns+') ) AS pvt'
exec (@sql)

DROP TABLE #Temp1
2021-03-17