- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经浏览了 Stackoverflow、在线图书和 Google 上的大量 SQL Pivot 示例,但我仍然不知道如何执行(我称之为)简单的 Pivot 操作。
示例数据:
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
Name Chinese English Biology Maths
======= ========== ======== ======= ======
Nick 80 70 85 85
Kent 80 70 85 90
注意:
In my head i imagine the syntax would be:
SELECT Score FROM Scores
GROUP BY Name
PIVOT BY Class
Name Chinese English Biology Maths
======= ========== ======== ======= ======
70 Nick
70 Kent
80 Nick
80 Kent
85 Nick Nick
85 Kent
90 Kent
注意:
In my head i imagine the syntax would be:
SELECT Name FROM Scores
GROUP BY Score
PIVOT BY Class
Name 70 80 85 90
======= ========== ======== ======= =====
Nick English Chinese Biology
Nick English Chinese Maths
Kent English Chinese Biology Maths
注意:
In my head i imagine the syntax would be:
SELECT Class FROM Scores
GROUP BY Name
PIVOT BY Score
Class 70 80 85 90
======= ========== ======== ======= =====
Chinese Nick
Chinese Kent
English Nick
English Kent
Biology Nick
Biology Kent
Maths Nick Kent
In my head i imagine the syntax would be:
SELECT Name FROM Scores
GROUP BY Class
PIVOT BY Score
Class Nick Kent
======= ==== ====
Chinese 80 80
English 70 70
Biology 85 85
Maths 85 90
In my head i imagine the syntax would be:
SELECT Score FROM Scores
GROUP BY Class
PIVOT BY Name
Score Nick Kent
===== ======= =======
70 English English
80 Chinese Chinese
85 Biology Biology
85 Maths Biology
90 Maths
In my head i imagine the syntax would be:
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
Username 20120901 20120902 20120903 20120914
======== ========= ======== ======== ========
iboyd obsidian obsidian obsidian harpax
iboyd obsidian harpax obsidian harpax
nbach president president
nback nichpc president
In my head i imagine the syntax would be:
SELECT MachineName FROM Logins
GROUP BY Username
PIVOT BY CONVERT(varchar(50), LoginDate, 112) --yyyymmdd formatOr perhaps:
SELECT MachineName FROM Logins
GROUP BY Username
PIVOT BY CAST(LoginDate AS DATE)
我似乎无法理解 PIVOT
语法;为了告诉 SQL Server 哪些列值应该成为列,以及聚合发生在哪些列值上。
每个人似乎都想对列进行硬编码,或者调用一些 XML 查询。我只是想做一个枢轴!
<小时/>我今天要解决的真正问题是“业务”给我的屏幕截图模型:
如果 SQL Server 语法对我来说相当明显,那么这可能是一个相当明显的查询:
SELECT
JobName, ShiftName,
Firstname+' '+Lastname+' - '+BankCode
FROM Transactions
GROUP BY JobName, ShiftName
PIVOT BY TransactionDate
最佳答案
将枢轴运算符视为替换您的组。以下是示例 #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
关于sql-server - 如何将列值转换为列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12287240/
我是一名优秀的程序员,十分优秀!