当前位置:网站首页>Application of crosstab in SQL Server
Application of crosstab in SQL Server
2022-07-26 10:17:00 【shin_ cy】
In the actual development process of the project , We sometimes encounter the problem of crosstab , for instance . Let's say there's a table T_ENT_EXHIBITION_SEATS , The structure and demonstration data are as follows :
ENT_NAME | EXHIBITION_NAME | EXHIBITION_SEATS |
ENTA | 105-1 | 5 |
ENTA | 105-2 | 7 |
ENTA | 106-1 | 13 |
ENTB | 105-2 | 2 |
ENTC | 105-1 | 18 |
ENTC | 106-2 | 7 |
ENTD | 105-2 | 16 |
ENTE | 106-1 | 3 |
According to the above data sheet and data , We need to get EXHIBITION_NAME Data of becomes a column , Get the following structure and data :
ENT_NAME | 105-1 | 105-2 | 106-1 | 106-2 |
ENTA | 5 | 7 | 13 | 0 |
ENTB | 0 | 2 | 0 | 0 |
ENTC | 18 | 0 | 0 | 7 |
ENTD | 0 | 16 | 0 | 0 |
ENTE | 0 | 0 | 3 | 0 |
stay SQL Server in , According to different versions , We can have different solutions , With SQL Server 2005 As a watershed , Let's start with SQL Server 2000 How to solve this problem . Usually we say that the data column generated by the crosstab ( image EXHIBITION_NAME) It can be data with a certain range unchanged , There is also dynamic data that can be maintained , The columns generated by the former are fixed , The latter can change with the increase or decrease of maintenance data , Different situations are handled differently , Solve the former first , Relatively simple , We can use CASE Statement to complete :
SELECT ENT_NAME,
ISNULL(CASE WHEN EXHIBITION_NAME = '105-1'
THEN EXHIBITION_SEATS
END, 0) AS EXHIBITION_NAME,
ISNULL(CASE WHEN EXHIBITION_NAME = '105-2'
THEN EXHIBITION_SEATS
END, 0) AS EXHIBITION_NAME,
ISNULL(CASE WHEN EXHIBITION_NAME = '106-1'
THEN EXHIBITION_SEATS
END, 0) AS EXHIBITION_NAME,
ISNULL(CASE WHEN EXHIBITION_NAME = '106-2'
THEN EXHIBITION_SEATS
END, 0) AS EXHIBITION_NAME
FROM T_ENT_EXHIBITION_SEATS
ORDER BY ENT_NAME ASC
If the data column is changing , Then we need to use dynamic SQL Here we go :
DECLARE @DynamicPIVOT NVARCHAR(4000)
SELECT @DynamicPIVOT = 'SELECT ENT_NAME'
SELECT @DynamicPIVOT = @DynamicPIVOT +
', ISNULL(SUM(CASE WHEN EXHIBITION_NAME = ''' + EXHIBITION_NAME +
''' THEN EXHIBITION_SEATS END), 0) AS ''' + EXHIBITION_NAME + ''''
FROM T_ENT_EXHIBITION_SEATS
GROUP BY EXHIBITION_NAME
ORDER BY EXHIBITION_NAME ASC
SET @DynamicPIVOT = @DynamicPIVOT + ' FROM T_ENT_EXHIBITION_SEATS GROUP BY ENT_NAME '
EXECUTE(@DynamicPIVOT)
here we are SQL Server 2005 perhaps SQL Server 2008, Microsoft has provided us with PIVOT To get the job done , Especially in the case of fixed data columns , Especially simple :
SELECT ENT_NAME,
ISNULL([105-1], 0) AS [105-1],
ISNULL([105-2], 0) AS [105-2],
ISNULL([106-1], 0) AS [106-1],
ISNULL([106-2], 0) AS [106-2]
FROM T_ENT_EXHIBITION_SEATS
PIVOT (
SUM(EXHIBITION_SEATS)
FOR EXHIBITION_NAME IN ([105-1], [105-2], [106-1], [106-2])
) AS PVT
ORDER BY ENT_NAME;
It should be noted that , The data column of the query must be the same as IN Clear scope corresponds to ( The order is irrelevant ). as for , Dynamic data columns , Still dynamic SQL To complete . How to write , I don't want to go into it .
About PIVOT You can refer to the following links for the use of :
http://technet.microsoft.com/en-us/library/ms177410(SQL.90).aspx
边栏推荐
- Usage of the formatter attribute of El table
- Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
- Wechat applet learning notes 1
- The fourth week of summer vacation
- 网易云UI模仿-->侧边栏
- Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
- Learning about tensor (III)
- 简单化构造函数的继承方法(二)- ES6中的class继承
- Study on the basis of opencv
- 万字详解“用知识图谱驱动企业业绩增长”
猜你喜欢
El table implements adding / deleting rows, and a parameter changes accordingly
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
【Halcon视觉】阈值分割
Production of a-modal drag function in antui
Use of tabbarcontroller
数通基础-Telnet远程管理设备
新建福厦铁路全线贯通 这将给福建沿海带来什么?
I finished watching this video on my knees at station B
数通基础-STP原理
AirTest
随机推荐
Leetcode 504. Hex number
The charm of SQL optimization! From 30248s to 0.001s
[qualcomm][network] QTI service analysis
Flask框架初学-04-flask蓝图及代码抽离
【Halcon视觉】极坐标变换
Draw arrows with openlayer
Principle analysis and source code interpretation of service discovery
Wechat applet learning notes 2
点赞,《新程序员》电子书限时免费领啦!
How to write a million reading article
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
AirTest
利用原生js实现自定义滚动条(可点击到达,拖动到达)
Data communication foundation STP principle
RecyclerView最后一条显示不全或显示部分的问题解决
分布式网络通信框架:本地服务怎么发布成RPC服务
Phpexcel export Emoji symbol error
输入整数后输入整行字符串的解决方法
数据库的复习--1.概述
The problem of four columns of Hanoi Tower