Dynamic Pivot table wizard SQL Server
原文 http://www.gyurcit.hu/pivot.html
Dynamic Pivot table wizard
This stored procedure generate dynamic crosstable with multiple pivot columns by 4+1 parameters.
1. P_Row_Field | Name of field that reperesent the spreadsheet's rows |
2. P_Column_Field | Name of field that store the name of columns |
3. P_Value | Name of field that represent the spreedsheet's value |
4. P_From | Name of source table |
5. P_Where | Filter expression (optional) |
Values of second parameter field should not include apastrophe. Procedure does not check paramaters (you must do that), so it may causes sql injection attack.
To source code click one of them
-- =================================================
-- Pivot Table Wizard (c) 2009 by GyurcIT
-- http://www.gyurcit.hu e-mail: gyurcit@gmail.com
-- ================================================= IF EXISTS (SELECT name FROM sysobjects
WHERE name = N'PivotWizard' AND type = 'P')
DROP PROCEDURE PivotWizard
GO
-----------------------------------------------------
-----------------------------------------------------
CREATE PROCEDURE PivotWizard
@P_Row_Field VARCHAR(255),
@P_Column_Field VARCHAR(255),
@P_Value VARCHAR(255),
@P_From VARCHAR(4000),
@P_Where VARCHAR(4000) = '1=1'
AS DECLARE @SQL NVARCHAR(4000) -- Build SQL statment that upload @Columns string
-- with @P_Column_Filed values
CREATE TABLE #TEMP (ColumnField varchar(100))
SET @sql ='SELECT DISTINCT '+@P_Column_Field+' AS ColumnField'+
' FROM '+@P_From+
' WHERE '+@P_Where+
' ORDER BY '+@P_Column_Field
INSERT INTO #TEMP
EXEC(@sql)
PRINT @sql -- Check count of columns
DECLARE @Count_Columns int
SELECT @Count_Columns = COUNT(*) FROM #Temp
IF (@Count_Columns<1) OR (@Count_Columns>255) BEGIN
DROP TABLE #Temp
RAISERROR('%d is invalid columns amount. Valid is 1-255',
16,1,@Count_columns)
RETURN
END
-- Upload @Columns from #Temp
DECLARE @Columns VARCHAR(8000),
@Column_Field VARCHAR(8000) SET @Columns = ''
DECLARE Column_cursor CURSOR LOCAL FOR
SELECT CAST(ColumnField AS VARCHAR(60))
FROM #Temp
OPEN Column_cursor
FETCH NEXT FROM Column_cursor
INTO @Column_Field
WHILE @@FETCH_STATUS = 0 BEGIN
SET @Columns = @Columns +
' SUM('+
' CASE WHEN '+@P_Column_Field+'='''+ @Column_Field+''''+
' THEN '+@P_Value+
' ELSE 0 END'+
') AS ['+ @Column_Field +'], '
FETCH NEXT FROM Column_cursor
INTO @Column_Field
END
CLOSE Column_cursor
DEALLOCATE Column_cursor
DROP TABLE #Temp IF @Columns='' RETURN 1
SET @Columns = Left(@Columns,Len(@Columns)-1) -- Build Pivot SQL statment
DECLARE @Pivot_SQL VARCHAR(8000)
SET @Pivot_SQL = 'SELECT ' +@P_Row_Field+', '+@Columns
SET @Pivot_SQL = @Pivot_SQL +' FROM ' +@P_From
SET @Pivot_SQL = @Pivot_SQL +' WHERE ' +@P_Where
SET @Pivot_SQL = @Pivot_SQL +' GROUP BY '+@P_Row_Field
SET @Pivot_SQL = @Pivot_SQL +' ORDER BY '+@P_Row_Field
SET @Pivot_SQL = @Pivot_SQL + '#' IF Right(@Pivot_SQL,1)<>'#'
BEGIN
RAISERROR('SQL statement is too long. It must be less
than 8000 charachter!',16,1)
RETURN 1
END
SET @Pivot_SQL = Left(@Pivot_SQL,Len(@Pivot_SQL)-1) -- PRINT @Pivot_SQL
EXEC(@Pivot_SQL) RETURN 0
GO -- Example use Northwind database --
-- Example 1 --
exec PivotWizard 'ShipCountry',
'YEAR(OrderDate)*100+Month(OrderDate)',
'[ExtendedPrice]',
'dbo.Invoices',
'OrderDate BETWEEN ''1996/01/01'' and ''1996/12/31''' GO -- Example 2 --
exec PivotWizard 'ProductName',
'ShipCountry',
'[ExtendedPrice]',
'dbo.Invoices',
'OrderDate BETWEEN ''1996/01/01'' and ''1996/12/31'''
-- =================================================
-- Pivot Table Wizard MySQL (c) 2009 by GyurcIT
-- http://www.gyurcit.hu e-mail: gyurcit@gmail.com
-- =================================================
-- USE Database
DROP PROCEDURE IF EXISTS pivotwizard; CREATE DEFINER = 'root'@'192.168.1.119'
PROCEDURE pivotwizard(
IN P_Row_Field VARCHAR(255),
IN P_Column_Field VARCHAR(255),
IN P_Value VARCHAR(255),
IN P_From VARCHAR(4000),
IN P_Where VARCHAR(4000))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE M_Count_Columns int DEFAULT 0;
DECLARE M_Column_Field varchar(60);
DECLARE M_Columns VARCHAR(8000) DEFAULT '';
DECLARE M_sqltext VARCHAR(8000);
DECLARE M_stmt VARCHAR(8000);
DECLARE cur1 CURSOR FOR SELECT CAST(Column_Field AS CHAR) FROM Temp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; DROP TABLE IF EXISTS Temp;
SET @M_sqltext = CONCAT('CREATE TEMPORARY TABLE Temp ',
' SELECT DISTINCT ',P_Column_Field,
' AS Column_Field',
' FROM ',P_From,
' WHERE ',P_Where,
' ORDER BY ', P_Column_Field); PREPARE M_stmt FROM @M_sqltext;
EXECUTE M_stmt; SELECT COUNT(*) INTO M_Count_Columns
FROM Temp
WHERE Column_Field IS NOT NULL; IF (M_Count_Columns > 0) THEN
OPEN cur1;
REPEAT
FETCH cur1 INTO M_Column_Field;
IF (NOT done) and (M_Column_Field IS NOT NULL) THEN
SET M_Columns = CONCAT(M_Columns,
' SUM( CASE WHEN ',P_Column_Field,'=''',M_Column_Field,'''',
' THEN ',P_Value,
' ELSE 0 END) AS `', M_Column_Field ,'`,'); END IF;
UNTIL done END REPEAT;
SET M_Columns = Left(M_Columns,Length(M_Columns)-1);
SET @M_sqltext = CONCAT('SELECT ',P_Row_Field,',',M_Columns,
' FROM ', P_From,
' WHERE ', P_Where,
' GROUP BY ', P_Row_Field,
' ORDER BY ', P_Row_Field); PREPARE M_stmt FROM @M_sqltext;
EXECUTE M_stmt;
END IF;
END
-- Example:
CALL pivotwizard('article','Month(date)',
'netto',
'invoice',
'(year(date)=2009)')
Dynamic Pivot table wizard SQL Server的更多相关文章
- Microsoft: Get started with Dynamic Data Masking in SQL Server 2016 and Azure SQL
Dynamic Data Masking (DDM) is a new security feature in Microsoft SQL Server 2016 and Azure SQL DB. ...
- How can I list all foreign keys referencing a given table in SQL Server?
How can I list all foreign keys referencing a given table in SQL Server? how to check if columns in ...
- [转]How can I list all foreign keys referencing a given table in SQL Server?
本文转自:https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given- ...
- SQL Server里PIVOT运算符的”红颜祸水“
在今天的文章里我想讨论下SQL Server里一个特别的T-SQL语言结构——自SQL Server 2005引入的PIVOT运算符.我经常引用这个与语言结构是SQL Server里最危险的一个——很 ...
- SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表)
原文:SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表) 作为SQL Server 2016(CTP3.x)的另一 ...
- Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...
- 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...
- Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]
http://sqlserverbuilds.blogspot.jp/ What version of SQL Server do I have? This unofficial build ch ...
- Microsoft SQL Server Version List(SQL Server 版本)
原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...
随机推荐
- Python之路Day13
day13主要内容:JavaScript.DOM.jQuery 武Sir blog:http://www.cnblogs.com/wupeiqi/articles/5369773.html JavaS ...
- perl encode_utf8 和decode_utf8
encode_utf8 等于 $octets = encode_utf8($string); 这个字符串 在$string 在Perl的内部格式,返回结果是作为一个顺序的字节. 因为所有的可能的字符串 ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- uvalive 2322 Wooden Sticks(贪心)
题目连接:2322 Wooden Sticks 题目大意:给出要求切的n个小木棍 , 每个小木棍有长度和重量,因为当要切的长度和重量分别大于前面一个的长度和重量的时候可以不用调整大木棍直接切割, 否则 ...
- Android自定义控件实战——水流波动效果的实现WaveView
转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38556891 水流波动的波形都是三角波,曲线是正余弦曲线,但是Android ...
- hdu4513之manacher算法
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
- 新安装的linux(linux mint 或则ubuntu)系统中安装postgresql-xc安装的包
一:./configure的时候1,gcc的处理:sudo apt-get install clang && rvm install 1.9.3 --with-gcc=clang2,缺 ...
- 2080夹角有多大II
寻人启事:2014级新生看过来! 夹角有多大II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- CentOS 6 用SVN自动提交文件到web服务器
关于 svn 的安装 参考:[转]Linux(centOS6.5)下SVN的安装.配置及开机启动 经过两天的各种尝试总算解决了,总结如下: 1.在建立库时注意 要让库的名称和 要同步的 web目录名 ...
- Week4(9月30日):
Part I:提问 =========================== 1.什么是DRY? 2.解释下面的模型验证规则. public class Movie { public int ID { ...