当前位置:网站首页>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
边栏推荐
- Like, "new programmer" e-book is free for a limited time!
- Li Kou - binary tree pruning
- Nodejs service background execution (forever)
- [Qualcomm][Network] qti服务分析
- Server memory failure prediction can actually do this!
- Usage of the formatter attribute of El table
- Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
- 【Halcon视觉】图像灰度变化
- Principle analysis and source code interpretation of service discovery
- Show default image when wechat applet image cannot be displayed
猜你喜欢

Keeping alive to realize MySQL automatic failover

Introduction to latex, EPS picture bounding box

Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills

数据库的复习--1.概述

Server memory failure prediction can actually do this!

PMM (percona monitoring and management) installation record

The charm of SQL optimization! From 30248s to 0.001s

Wechat applet learning notes 1

Data communication foundation TCPIP reference model

Draw arrows with openlayer
随机推荐
Use of Android grendao database
Android greendao数据库的使用
Uniapp error 7 < Map >: marker ID should be a number
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
Sublime install plug-ins
Study notes of the first week of sophomore year
About automatic operation on Web pages
Learning about opencv (2)
Solution of inputting whole line string after inputting integer
Data communication foundation TCPIP reference model
Getting started with SQL - combined tables
Wechat H5 payment on WAP, for non wechat browsers
Tower of Hanoi II | tower of Hanoi 4 columns
Self encapsulated database dbutils universal template
Principle analysis and source code interpretation of service discovery
面试突击68:为什么 TCP 需要 3 次握手?
在.NET 6.0中配置WebHostBuilder
Under win10 64 bit, matlab fails to configure notebook
输入整数后输入整行字符串的解决方法
Introduction to latex, EPS picture bounding box