/*创建数据库*/
CREATE DATABASE tmp
go
USE tmp
go
/*创建数据库测试表*/
CREATE TABLE [Scores]
(
[ID] INT IDENTITY(1, 1)
PRIMARY KEY ,
[Student] VARCHAR(20) ,
[Subject] VARCHAR(30) ,
[Score] FLOAT
)
go
TRUNCATE TABLE Scores
/*插入数据库测试数据信息*/
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'test001', '语文', '90' )
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'test001', '英语', '85' )
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'text002', '语文', '90' )
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'text002', '英语', '80' )
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'test003', '语文', '95' )
INSERT INTO Scores
( Student, Subject, Score )
VALUES ( 'test003', '英语', '85' )
/*1. case when .......then else ....end 用法,行列转换*/
SELECT Student AS '姓名' ,
MAX(CASE Subject
WHEN '语文' THEN Score
ELSE 0
END) AS '语文' ,--如果这个行是“语文”,就选此行作为列
MAX(CASE Subject
WHEN '英语' THEN Score
ELSE 0
END) AS '英语'
FROM Scores
GROUP BY Student
ORDER BY Student
/*2. pivot(聚合函数(要转成列值的列名)
for 要转换的列
in(目标列名)
)*/
SELECT Student AS '姓名' ,
AVG(语文) AS '语文' ,
AVG(英语) AS '英语'
FROM Scores PIVOT( AVG(Score) FOR Subject IN ( 语文, 英语 ) )as NewScores
GROUP BY Student
ORDER BY Student ASC
sql,case语句,pivot
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]