-- 你这太长了,写了个简单的例子
WITH a AS
(SELECT 1 c1, 'a' c2, 1 c3 FROM dual
UNION ALL
SELECT 1 c1, 'b' c2, 2 c3 FROM dual
UNION ALL
SELECT 2 c1, 'c' c2, 1 c3 FROM dual
UNION ALL
SELECT 2 c1, 'd' c2, 3 c3 FROM dual)
SELECT
c1,
max(CASE WHEN rnum = 1 THEN c2 END ) c2_1,
max(CASE WHEN rnum = 2 THEN c2 END ) c2_2,
max(CASE WHEN rnum = 1 THEN c3 END ) c3_1,
max(CASE WHEN rnum = 2 THEN c3 END ) c3_2 FROM
(
SELECT a.*, row_number() OVER(PARTITION BY c1 ORDER BY c2) rnum FROM a
) a1
GROUP BY c1;
C1 C2_1 C2_2 C3_1 C3_2
---------- ---- ---- ---------- ----------
1 a b 1 2
2 c d 1 3
[/code]
#9
-- 你这太长了,写了个简单的例子
WITH a AS
(SELECT 1 c1, 'a' c2, 1 c3 FROM dual
UNION ALL
SELECT 1 c1, 'b' c2, 2 c3 FROM dual
UNION ALL
SELECT 2 c1, 'c' c2, 1 c3 FROM dual
UNION ALL
SELECT 2 c1, 'd' c2, 3 c3 FROM dual)
SELECT
c1,
max(CASE WHEN rnum = 1 THEN c2 END ) c2_1,
max(CASE WHEN rnum = 2 THEN c2 END ) c2_2,
max(CASE WHEN rnum = 1 THEN c3 END ) c3_1,
max(CASE WHEN rnum = 2 THEN c3 END ) c3_2 FROM
(
SELECT a.*, row_number() OVER(PARTITION BY c1 ORDER BY c2) rnum FROM a
) a1
GROUP BY c1;
C1 C2_1 C2_2 C3_1 C3_2
---------- ---- ---- ---------- ----------
1 a b 1 2
2 c d 1 3
-- 你这太长了,写了个简单的例子
WITH a AS
(SELECT 1 c1, 'a' c2, 1 c3 FROM dual
UNION ALL
SELECT 1 c1, 'b' c2, 2 c3 FROM dual
UNION ALL
SELECT 2 c1, 'c' c2, 1 c3 FROM dual
UNION ALL
SELECT 2 c1, 'd' c2, 3 c3 FROM dual)
SELECT
c1,
max(CASE WHEN rnum = 1 THEN c2 END ) c2_1,
max(CASE WHEN rnum = 2 THEN c2 END ) c2_2,
max(CASE WHEN rnum = 1 THEN c3 END ) c3_1,
max(CASE WHEN rnum = 2 THEN c3 END ) c3_2 FROM
(
SELECT a.*, row_number() OVER(PARTITION BY c1 ORDER BY c2) rnum FROM a
) a1
GROUP BY c1;
C1 C2_1 C2_2 C3_1 C3_2
---------- ---- ---- ---------- ----------
1 a b 1 2
2 c d 1 3
[/code]
#9
-- 你这太长了,写了个简单的例子
WITH a AS
(SELECT 1 c1, 'a' c2, 1 c3 FROM dual
UNION ALL
SELECT 1 c1, 'b' c2, 2 c3 FROM dual
UNION ALL
SELECT 2 c1, 'c' c2, 1 c3 FROM dual
UNION ALL
SELECT 2 c1, 'd' c2, 3 c3 FROM dual)
SELECT
c1,
max(CASE WHEN rnum = 1 THEN c2 END ) c2_1,
max(CASE WHEN rnum = 2 THEN c2 END ) c2_2,
max(CASE WHEN rnum = 1 THEN c3 END ) c3_1,
max(CASE WHEN rnum = 2 THEN c3 END ) c3_2 FROM
(
SELECT a.*, row_number() OVER(PARTITION BY c1 ORDER BY c2) rnum FROM a
) a1
GROUP BY c1;
C1 C2_1 C2_2 C3_1 C3_2
---------- ---- ---- ---------- ----------
1 a b 1 2
2 c d 1 3