在https://www.itjungle.com/2015/04/21/fhg042115-story01/下可以找到一篇关于如何编写通用 Db2 数据透视方法的好文章。
执行以下代码(注意 'end #' 而不是 'end ;'):
SET SCHEMA = TESTSCHEMA;
CREATE PROCEDURE DO_PIVOT
(IN FOR_SCHEMA CHARACTER (10) ,
IN FOR_TABLE CHARACTER (10) ,
IN PIVOT_COLUMN VARCHAR (250) ,
IN VALUE_COLUMN VARCHAR (250) ,
IN AGG_FUNCTION VARCHAR (5) DEFAULT 'SUM' ,
IN GROUP_COLUMN VARCHAR (250) DEFAULT NULL )
LANGUAGE SQL
MODIFIES SQL DATA
PROGRAM TYPE SUB
CONCURRENT ACCESS RESOLUTION DEFAULT
DYNAMIC RESULT SETS 1
OLD SAVEPOINT LEVEL COMMIT ON RETURN NO
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0 ;
DECLARE SQL_STATEMENT VARCHAR ( 5000 ) ;
DECLARE PIVOT_VALUE VARCHAR ( 20 ) ;
DECLARE PAD CHAR ( 2 ) DEFAULT ' ' ;
DECLARE C1 CURSOR FOR D1 ;
DECLARE C2 CURSOR WITH RETURN FOR D2 ;
SET SCHEMA = FOR_SCHEMA ;
-- Get the list of values available for the pivot column
-- Each value will be a column in the return set
SET SQL_STATEMENT = 'select distinct '
|| PIVOT_COLUMN
|| ' from '
|| FOR_TABLE
|| ' order by 1' ;
PREPARE D1 FROM SQL_STATEMENT ;
OPEN C1 ;
-- Construct a dynamic select statement for the pivot
SET SQL_STATEMENT = 'select ' ;
-- If requested, add the Group By Column
-- to the select clause
IF GROUP_COLUMN IS NOT NULL THEN
SET SQL_STATEMENT = SQL_STATEMENT || GROUP_COLUMN ;
SET PAD = ', ' ;
END IF ;
-- For each possible value for the Pivot Column,
-- add a case statement to perform the requested
-- aggregate function on the Value Column
FETCH NEXT FROM C1 INTO PIVOT_VALUE ;
WHILE ( SQLCODE >= 0 AND SQLCODE <> 100 ) DO
SET SQL_STATEMENT = SQL_STATEMENT
|| PAD
|| AGG_FUNCTION
|| '(CASE WHEN '
|| PIVOT_COLUMN
|| ' = '''
|| PIVOT_VALUE
|| ''' THEN '
|| VALUE_COLUMN
|| ' END) AS '
|| PIVOT_VALUE ;
SET PAD = ', ' ;
FETCH NEXT FROM C1 INTO PIVOT_VALUE ;
END WHILE ;
CLOSE C1 ;
-- Specify the table to select from
SET SQL_STATEMENT = SQL_STATEMENT
|| ' from '
|| FOR_TABLE ;
-- If requested, add the Group By Column
-- to the select clause
IF GROUP_COLUMN IS NOT NULL THEN
SET SQL_STATEMENT = SQL_STATEMENT
|| ' group by '
|| GROUP_COLUMN
|| ' order by '
|| GROUP_COLUMN;
END IF ;
PREPARE D2 FROM SQL_STATEMENT ;
OPEN C2 ;
END #
LABEL ON ROUTINE DO_PIVOT
( CHAR(), CHAR(), VARCHAR(), VARCHAR(), VARCHAR(), VARCHAR() )
IS 'Perform a General Purpose Pivot';
COMMENT ON PARAMETER ROUTINE DO_PIVOT
( CHAR(), CHAR(), VARCHAR(), VARCHAR(), VARCHAR(), VARCHAR() )
(FOR_SCHEMA IS 'Schema for Table' ,
FOR_TABLE IS 'For Table' ,
PIVOT_COLUMN IS 'Name of Column to be Pivoted' ,
VALUE_COLUMN IS 'Column to be Aggregated for Pivot' ,
AGG_FUNCTION IS 'Use Aggregate Function' ,
GROUP_COLUMN IS 'Group on Column' ) ;
产生错误消息:
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "FROM" was found following " FETCH
NEXT".
Expected tokens may include: ".". LINE NUMBER=50. SQLSTATE=42601
- 该过程似乎是针对可能在 iSeries 上的较旧 Db2 版本进行编程的
- 关于当前的 LUW 版本(10.5、11.1),代码片段中“获取下一个”的语法乍一看似乎是正确的:https ://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com .ibm.db2.luw.sql.ref.doc/doc/r0000954.html
- 在 Db2 Big SQL 4.2 上测试(“DB2 v11.1.0.0”、“s160705”、“BSQL160705AMD64”和 Fix Pack “0”)
有什么建议么?