当前位置:网站首页>SQL Server row to column (pivot), column to row (unpivot)
SQL Server row to column (pivot), column to row (unpivot)
2022-06-10 22:04:00 【Game programming】
explain
have access to PIVOT and UNPIVOT Relational operators change a table valued expression to another table . PIVOT By converting the unique value of one column in the expression to multiple columns in the output , To rotate table valued expressions . PIVOT Run aggregation when you need to aggregate all the remaining column values required for the final output . And PIVOT Reverse operation ,UNPIVOT Rotate the columns of a table valued expression into column values .
PIVOT The syntax provided is more complex than a series of SELECT…CASE The syntax specified in the statement is simpler and more readable .
grammar
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 ( Transfer line column )
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 ' height '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 pThe original table
id num1 1572 1823 1754 1655 178pivot after
highs 1 2 3 4 5 height 157 182 175 165 178Column turned (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 The original table
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 after
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 5author : Turbid dust
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- 信号与系统复习1
- Anti shake and throttling
- What should be paid attention to when designing Multilayer PCB?
- C language ---6 first knowledge of selection statement, loop statement, function and array
- 入行须知:运维需要懂编程吗?
- ThinkPHP v6.0. X deserialization vulnerability recurrence
- Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
- Redis cache penetration
- H265 Nalu类型判断及 sps 数据解析
- Asynchronous, thread pool (completablefuture)
猜你喜欢

Redis cache breakdown

C language -- 1 c language cognition

Understanding of related concepts of target detection

2022-06-09 RK817 PMU 電池溫度檢測

「运维有小邓」自助帐户解锁工具

2022-06-09 rk817 PMU battery temperature detection

App test case
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)

你的公司会选择开发数据中台吗?

登堂入室之soc开发环境及硬件开发准备
随机推荐
Notes to entry: do I need to know programming for O & M?
C language ---9 first knowledge of macros and pointers
[nk] 牛客月赛51 F-平均题
How to view the occupied space of a table in MySQL database
C language ---2 initial knowledge of data types
Quick start to VISSIM simulation
Constructing the implementation strategy of steam education for children
The process of keyword search in Oracle tables
LeetCode 进阶之路 - 136.只出现一次的数字
设计多层PCB板需要注意哪些事项?
Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
Qingniao Changping campus of Peking University: can I learn UI with a high school degree?
旋转导航栏
Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
Abbexa AML1 DNA binding ELISA Kit instructions
Abbexa cell free DNA kit instructions
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated
C language -- 1 c language cognition
Interpreting the new ecology of education in maker space
Abbexa acrylamide peg NHS instructions