10

我已经浏览了 Stackoverflow、在线图书和 google 上的很多 SQL Pivot 示例,但我仍然无法弄清楚如何执行(我称之为)简单的数据透视操作。

示例 1

样本数据:

Name     Class       Score
=======  ==========  ======
Nick     Chinese     80 
Nick     English     70 
Nick     Biology     85 
Nick     Maths       85
Kent     Chinese     80 
Kent     Maths       90 
Kent     English     70 
Kent     Biology     85 

所需的输出 1 - 按类透视,按名称聚合

Name     Chinese     English   Biology  Maths
=======  ==========  ========  =======  ======
Nick     80          70        85       85
Kent     80          70        85       90

笔记:

在我的脑海中,我想语法是:

SELECT Score FROM Scores
GROUP BY Name
PIVOT BY Class

期望的输出 2 - 按类别透视,总分

Name     Chinese     English   Biology  Maths
=======  ==========  ========  =======  ======
70                   Nick
70                   Kent
80       Nick        
80       Kent             
85                             Nick     Nick
85                             Kent
90                                      Kent

笔记:

在我的脑海中,我想语法是:

SELECT Name FROM Scores
GROUP BY Score
PIVOT BY Class

期望的输出 3 - 按分数透视,按名称聚合

Name     70          80        85       90
=======  ==========  ========  =======  =====
Nick     English     Chinese   Biology  
Nick     English     Chinese   Maths
Kent     English     Chinese   Biology  Maths

笔记:

在我的脑海中,我想语法是:

SELECT Class FROM Scores
GROUP BY Name
PIVOT BY Score

期望的输出 4 - 按分数透视,按类别汇总

Class    70          80        85       90
=======  ==========  ========  =======  =====
Chinese              Nick
Chinese              Kent
English  Nick
English  Kent
Biology                        Nick
Biology                        Kent
Maths                          Nick     Kent

在我的脑海中,我想语法是:

SELECT Name FROM Scores
GROUP BY Class 
PIVOT BY Score

所需输出 5 - 按名称透视,按类别聚合

Class    Nick  Kent
=======  ====  ====
Chinese  80    80
English  70    70
Biology  85    85
Maths    85    90

在我的脑海中,我想语法是:

SELECT Score FROM Scores
GROUP BY Class
PIVOT BY Name

期望的输出 6 - 按名称透视,按分数汇总

Score  Nick     Kent
=====  =======  =======
70     English  English
80     Chinese  Chinese
85     Biology  Biology
85     Maths    Biology
90              Maths

在我的脑海中,我想语法是:

SELECT Class FROM Scores
GROUP BY Score
PIVOT BY Name

注意:我不想要一个可以执行所有这些数据透视的查询。我正在使用示例数据和示例枢轴,因此请用作我可能想要执行的枢轴的示例。

另一个示例集

另一个示例可能是解析用户登录域的日志:

LoginDate          Username  MachineName
=================  ========  ===========
20120901 8:49:22   iboyd     obsidian
20120901 9:10:19   nbach     president
20120901 13:07:18  nback     nichpc
20120902 8:58:38   iboyd     obsidian
20120202 9:14:44   nbach     president
20120902 18:34:43  iboyd     harpax
20120903 8:57:13   iboyd     obsidian
20120904 20:03:55  iboyd     harpax

所需的输出 7 - 按 LoginDate 的日期部分透视,按用户名聚合:

Username  20120901   20120902  20120903  20120914
========  =========  ========  ========  ========
iboyd     obsidian   obsidian  obsidian  harpax
iboyd     obsidian   harpax    obsidian  harpax
nbach     president  president
nback     nichpc     president

在我的脑海中,我想语法是:

SELECT MachineName FROM Logins
GROUP BY Username
PIVOT BY CONVERT(varchar(50), LoginDate, 112) --yyyymmdd format

也许:

SELECT MachineName FROM Logins
GROUP BY Username
PIVOT BY CAST(LoginDate AS DATE)

我似乎无法理解PIVOT语法;为了告诉 SQL Server 哪些列值应该成为列,以及聚合发生在哪些列值上。

每个人似乎都想对列进行硬编码,或者调用一些 XML 查询。我只想做一个支点!


也可以看看


真正的问题TM

我今天要解决的真正问题是“业务”给我的屏幕截图模型:

在此处输入图像描述

如果 SQL Server 语法对我来说相当明显的话,这可能是一个相当明显的查询:

SELECT 
    JobName, ShiftName, 
    Firstname+' '+Lastname+' - '+BankCode
FROM Transactions
GROUP BY JobName, ShiftName
PIVOT BY TransactionDate
4

1 回答 1

4

将枢轴运算符视为替换您的组。这是您的示例 #1 和 3 的示例:

SELECT name, [Chinese], [English], [Biology], [Maths]
FROM scores s
PIVOT (
    SUM(score) 
    FOR Class IN ([Chinese], [English], [Biology], [Maths])
) p

SELECT name, [70], [80], [85], [90]
FROM scores s
PIVOT (
    MAX(class) 
    FOR score IN ([70], [80], [85], [90])
) p
于 2012-09-24T18:34:40.580 回答