当前位置:网站首页>sql server行转列(pivot),列转行(unpivot)
sql server行转列(pivot),列转行(unpivot)
2022-06-10 20:45:00 【游戏编程】
说明
可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表。 PIVOT 通过将表达式中的一个列的唯一值转换为输出中的多列,来轮替表值表达式。 PIVOT 在需要对最终输出所需的所有剩余列值执行聚合时运行聚合。 与 PIVOT 执行的操作相反,UNPIVOT 将表值表达式的列轮换为列值。
PIVOT 提供的语法比一系列复杂的 SELECT…CASE 语句中所指定的语法更简单和更具可读性。
语法
SELECT <non-pivoted column>, [first pivoted column] AS <column name>, [second pivoted column] AS <column name>, ... [last pivoted column] AS <column name> FROM (<SELECT query that produces the data>) AS <alias for the source query> PIVOT ( <aggregation function>(<column being aggregated>) FOR [<column that contains the values that will become column headers>] IN ( [first pivoted column], [second pivoted column], ... [last pivoted column]) ) AS <alias for the pivot table> <optional ORDER BY clause>;PIVOT (行转列)
create table #person(id int,num int)insert into #person values(1,157)insert into #person values(2,182)insert into #person values(3,175)insert into #person values(4,165)insert into #person values(5,178)select *from #personselect '身高'as highs ,[1],[2],[3],[4],[5] from (select id,num from #person) s pivot (avg(num)foR id in([1],[2],[3],[4],[5]) ) as p原表
id num1 1572 1823 1754 1655 178pivot 后
highs 1 2 3 4 5身高 157 182 175 165 178列转行(unpivot)
CREATE TABLE pvt (VendorID INT, Emp1 INT, Emp2 INT, Emp3 INT, Emp4 INT, Emp5 INT); GO INSERT INTO pvt VALUES (1,4,3,5,4,4); INSERT INTO pvt VALUES (2,4,1,5,5,5); INSERT INTO pvt VALUES (3,4,3,5,4,4); INSERT INTO pvt VALUES (4,4,2,5,5,4); INSERT INTO pvt VALUES (5,5,1,5,5,5); GO select *from pvtSELECT VendorID, Employee, Orders FROM (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5 FROM pvt) p UNPIVOT (Orders FOR Employee IN (Emp1, Emp2, Emp3, Emp4, Emp5) )AS unpvt; GO 原表
VendorID Emp1 Emp2 Emp3 Emp4 Emp51 4 3 5 4 42 4 1 5 5 53 4 3 5 4 44 4 2 5 5 45 5 1 5 5 5unpivot 后
VendorID Employee Orders1 Emp1 41 Emp2 31 Emp3 51 Emp4 41 Emp5 42 Emp1 42 Emp2 12 Emp3 52 Emp4 52 Emp5 53 Emp1 43 Emp2 33 Emp3 53 Emp4 43 Emp5 44 Emp1 44 Emp2 24 Emp3 54 Emp4 54 Emp5 45 Emp1 55 Emp2 15 Emp3 55 Emp4 55 Emp5 5作者:浊尘
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- protoc protoc-gen-go protobuf 之间的关系
- LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
- 学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?
- 2022 - 06 - 09 rk817 PMU Battery Temperature Detection
- MySQL范围查询优化的场景实例详解
- Vissim仿真快速入门
- [qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
- Understanding deep learning attention
- Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated
猜你喜欢
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)

初中毕业生,选择中职学校也可以升入高等学府
CentOS7安装MySQL8的超级详细教程(无坑!)

C language -- 11 branch statement if else

Will your company choose to develop data center?

C language ---5 initial string, escape character and comment

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)

详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)

构建幼儿Steam教育实施策略
随机推荐
2021年平均工资出炉,IT行业不出所料
[untitled] broken item
C language -- 11 branch statement if else
自制Table表格
【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?
【无标题】破目
[NK] question de calcul de 51 g pour le match lunaire Bullock
Self made table
"O & M youxiaodeng" self service account unlocking tool
Is it safe to buy funds on mobile phones? Will the principal be swallowed?
在模仿学习中进步的智能机器人
Qingniao Changping campus of Peking University: can I learn UI with a high school degree?
Leetcode advanced path - reverse string
Course design of imitation pottery ticket of wechat applet
Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test
Codeforces Round #798 (Div. 2)
H265 Nalu类型判断及 sps 数据解析
SQL Server2019安装的详细步骤实战记录(亲测可用)
Quick start to VISSIM simulation
Leetcode advanced path - Search insertion location