所以我想要做的就是将行转换为列。以前在本网站上已经涵盖了这种方式,我最喜欢的方式如下:
SELECT
FRNPCM032.WFP.WFORD#,
MAX(CASE WHEN FRNPCM032.WFP.WFSEGN = 'COLOR1' THEN WFVAL END) AS COLOR,
MAX(CASE WHEN FRNPCM032.WFP.WFSEGN = 'OSKVA' THEN WFVAL END) AS KVA,
MAX(CASE WHEN FRNPCM032.WFP.WFSEGN = 'OSSWITCH' THEN WFVAL END) AS LBSWITCH
FROM FRNPCM032.WFP
GROUP BY FRNPCM032.WFP.WFORD#;
这是一种非常简单的方法,但是我需要一些不那么手动的东西,因为以后可能会添加新行,我不想再回去继续维护查询或查看。
有没有办法动态地做到这一点?我可以想办法用循环来做,但我不能在查询或视图中这样做。
答案 0 :(得分:0)
不幸的是,DB2 for IBM我没有PIVOT()函数......
但是,您可以构建一个动态查看表的当前内容的过程,并构建一个用于转动数据的SQL语句。
以下代码来自文章An SQL Pivot Procedure
如果您希望可以修改代码来构建视图,而不是返回结果集。但是,如果枢轴值发生变化,您必须重建视图。
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' ) ;