答案 0 :(得分:1)
在LUW的Db2上,它将起作用。大型机SQL将类似。只需使用您的表名代替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