内容简介:前几天一直练习PIVOT透视,还实现了动态透视的存过程《动态透视表》今天练习MS SQL Server逆透视的功能。首先准备一些可以逆透视的数据:
前几天一直练习PIVOT透视,还实现了动态透视的存过程《动态透视表》 https://www.cnblogs.com/insus/p/10888277.html
今天练习MS SQL Server逆透视的功能。
首先准备一些可以逆透视的数据:
IF OBJECT_ID('tempdb.dbo.#Part_summary') IS NOT NULL DROP TABLE #Part_summary CREATE TABLE #Part_summary ( [Item] NVARCHAR(40), [B]DECIMAL(18,2), [Q]DECIMAL(18,2), [S]DECIMAL(18,2), [T]DECIMAL(18,2), [U]DECIMAL(18,2) ) INSERT INTO #Part_summary ([Item],[B],[Q],[S],[T],[U]) VALUES ('098-SSSS1-WS0098-5526',0,0,500.00,0,0), ('54F-ART43-6545NN-2514',0,0,934.39,0,0), ('872-RTDE3-Q459PW-2323',0,0,0,452.44,0), ('B78-F1H2Y-5456UD-2530',0,0,0,115.06,0), ('I32-GG443-QT0098-0001',0,0,423.65,0,0), ('I38-AA321-WS0098-0506',470.87,0,0,0,0), ('K38-12321-5456UD-3493',200.28,0,0,0,0), ('PO0-7G7G7-JJY098-0077',0,871.33,0,0,0), ('RVC-43ASE-H43QWW-9753',0,0,0,0,555.19), ('X3C-SDEWE-3ER808-8764',0,607.88,0,0,0) SELECT [Item],[B],[Q],[S],[T],[U] FROM #Part_summary Source Code
下面是Insus.NET实现UNPIVOT的代码:
SELECT [Item] ,[Category] ,[Qty] FROM #Part_summary UNPIVOT ( [Qty] FOR [Category] IN ([B],[Q],[S],[T],[U]) ) AS [UNPIVOT TABLE] WHERE [Qty] > 0 Source Code
以上是以手动实现逆透视。如果想实现动态逆透视。可以参考下面的存储过程:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Insus.NET -- Create date: 2019-05-20 -- Update date: 2019-05-20 -- Description: 动态逆透视 CREATE PROCEDURE [dbo].[usp_Dynamic_UnPivot] ( @table_name SYSNAME, --透视的表名 @common_column SYSNAME, --常规共用列名 @unpivot_column SYSNAME, --逆透视列名 @sum_column SYSNAME, --计和的列名 @Comma_Delimited_Column_Names NVARCHAR(MAX) ) AS BEGIN DECLARE @sql AS NVARCHAR(MAX) = N' SELECT '+ @common_column +' ,'+ @unpivot_column +' ,[Qty] FROM '+ @table_name +' UNPIVOT ( '+ @sum_column +' FOR '+ @unpivot_column +' IN ('+ @Comma_Delimited_Column_Names +') ) AS [UNPIVOT TABLE] WHERE '+ @sum_column +' > 0' EXECUTE sp_executeSql @sql END GO Source Code
怎样使用这个存储过程呢,参考下面参数传入方法:
重复一下,存储过程的参数说明如下:
@table_name SYSNAME, --透视的表名 @common_column SYSNAME, --常规共用列名 @unpivot_column SYSNAME, --逆透视列名 @sum_column SYSNAME, --计和的列名 @Comma_Delimited_Column_Names NVARCHAR(MAX)
以上所述就是小编给大家介绍的《UNPIVOT逆透视以及动态逆透视存储过程》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Game Engine Architecture, Second Edition
Jason Gregory / A K Peters/CRC Press / 2014-8-15 / USD 69.95
A 2010 CHOICE outstanding academic title, this updated book covers the theory and practice of game engine software development. It explains practical concepts and techniques used by real game studios,......一起来看看 《Game Engine Architecture, Second Edition》 这本书的介绍吧!