DB2 Sql 用于大型机中的转置
DB2 Sql for transpose in Mainframe
want to achieve below the in mainframe Db2 Sql
在 LUW 的 Db2 上,这将起作用。大型机 SQL 将是相似的。只需使用您的 table 名称代替 TABLE(VALUES ... AS(..)
位
SELECT
MAX(CASE WHEN RN = 1 THEN Student_no END) AS Student_no_1
, MAX(CASE WHEN RN = 1 THEN S_Name END) AS S_Name_1
, MAX(CASE WHEN RN = 1 THEN Marks END) AS Marks_1
, MAX(CASE WHEN RN = 2 THEN Student_no END) AS Student_no_2
, MAX(CASE WHEN RN = 2 THEN S_Name END) AS S_Name_2
, MAX(CASE WHEN RN = 2 THEN Marks END) AS Marks_2
, MAX(CASE WHEN RN = 3 THEN Student_no END) AS Student_no_3
, MAX(CASE WHEN RN = 3 THEN S_Name END) AS S_Name_3
, MAX(CASE WHEN RN = 3 THEN Marks END) AS Marks_3
FROM
( SELECT ROW_NUMBER() OVER(ORDER BY Student_no) AS RN
, Student_no
, S_Name
, Marks
FROM
TABLE(VALUES( 2, 'Abc', 89),( 5, 'dfg', 35),( 9, 'qwe', 56))
AS (Student_no, S_Name, Marks)
)
STUDENT_NO_1 S_NAME_1 MARKS_1 STUDENT_NO_2 S_NAME_2 MARKS_2 STUDENT_NO_3 S_NAME_3 MARKS_3
------------ -------- ------- ------------ -------- ------- ------------ -------- -------
2 Abc 89 5 dfg 35 9 qwe 56
want to achieve below the in mainframe Db2 Sql
在 LUW 的 Db2 上,这将起作用。大型机 SQL 将是相似的。只需使用您的 table 名称代替 TABLE(VALUES ... AS(..)
位
SELECT
MAX(CASE WHEN RN = 1 THEN Student_no END) AS Student_no_1
, MAX(CASE WHEN RN = 1 THEN S_Name END) AS S_Name_1
, MAX(CASE WHEN RN = 1 THEN Marks END) AS Marks_1
, MAX(CASE WHEN RN = 2 THEN Student_no END) AS Student_no_2
, MAX(CASE WHEN RN = 2 THEN S_Name END) AS S_Name_2
, MAX(CASE WHEN RN = 2 THEN Marks END) AS Marks_2
, MAX(CASE WHEN RN = 3 THEN Student_no END) AS Student_no_3
, MAX(CASE WHEN RN = 3 THEN S_Name END) AS S_Name_3
, MAX(CASE WHEN RN = 3 THEN Marks END) AS Marks_3
FROM
( SELECT ROW_NUMBER() OVER(ORDER BY Student_no) AS RN
, Student_no
, S_Name
, Marks
FROM
TABLE(VALUES( 2, 'Abc', 89),( 5, 'dfg', 35),( 9, 'qwe', 56))
AS (Student_no, S_Name, Marks)
)
STUDENT_NO_1 S_NAME_1 MARKS_1 STUDENT_NO_2 S_NAME_2 MARKS_2 STUDENT_NO_3 S_NAME_3 MARKS_3
------------ -------- ------- ------------ -------- ------- ------------ -------- -------
2 Abc 89 5 dfg 35 9 qwe 56