UNPIVOT reverse perspective and dynamic reverse perspective stored procedure

I practiced PIVOT perspective the other day and realized the saving process of dynamic perspective< Dynamic PivotTable>https://www.cnblogs.com/insus/p/10888277.html

Today, I will practice the function of MS SQL Server reverse perspective.

First, prepare some data that can be inverted perspective:

 

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

 

Here is the code for Insus.NET to implement 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

 

The above is to realize inverse perspective manually. If you want to achieve dynamic inverse perspective. You can refer to the following stored procedures:

 

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      Insus.NET
-- Create date: 2019-05-20
-- Update date: 2019-05-20
-- Description: Dynamic inverse perspective
CREATE PROCEDURE  [dbo].[usp_Dynamic_UnPivot]
(        
    @table_name SYSNAME,           --Table name of pivot
    @common_column SYSNAME,        --Common column name
    @unpivot_column SYSNAME,       --Reverse perspective column name
    @sum_column SYSNAME,           --Column name of sum
    @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

 

How to use this stored procedure? Refer to the following parameter passing method:

 

Again, the parameters of the stored procedure are described as follows:

@Table name sysname, -- name of the pivot table
@Common column sysname, -- common column name
@Unpivot [column sysname, -- reverse perspective column name
@Sum column sysname, -- column name of sum
@Comma_Delimited_Column_Names NVARCHAR(MAX) 

Tags: SQL Sever SQL Stored Procedure

Posted on Thu, 07 Nov 2019 08:13:37 -0800 by Petrushka