2

当向 10g Oracle 数据库发出以下查询时:

    SELECT * FROM (
        SELECT T1.ID, T2.ACCT_NO, ROW_NUMBER() OVER (PARTITION BY T1.ID ORDER BY T1.ID DESC) AS RRRRRRR 
        FROM TABLE1 T1
        INNER JOIN TABLE T2 ON T1.ID = T2.ID
        WHERE T1.ID = 666
    )
    PIVOT (MIN(T1.ID) AS ALIAS1 FOR RRRRRRR IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

我收到“命令未正确结束”错误。我搜索了 Oracle 数据透视示例,它们几乎都显示了相同的示例。我在这里想念什么?

4

2 回答 2

3

正如@APC 指出的那样PIVOT,oracle 10g 中没有函数,因此您可以使用聚合函数和 a CASE,类似于:

SELECT id, acct_no, 
  min(case when RRRRRRR = 1 then id end) as '1',
  min(case when RRRRRRR = 2 then id end) as '2',
  min(case when RRRRRRR = 3 then id end) as '3',
  min(case when RRRRRRR = 4 then id end) as '4',
  min(case when RRRRRRR = 5 then id end) as '5',
  min(case when RRRRRRR = 6 then id end) as '6',
  min(case when RRRRRRR = 7 then id end) as '7',
  min(case when RRRRRRR = 8 then id end) as '8',
  min(case when RRRRRRR = 9 then id end) as '9',
  min(case when RRRRRRR = 10 then id end) as '10'
FROM 
(
  SELECT T1.ID, T2.ACCT_NO, ROW_NUMBER() OVER (PARTITION BY T1.ID ORDER BY T1.ID DESC) AS RRRRRRR 
  FROM TABLE1 T1
  INNER JOIN TABLE T2 ON T1.ID = T2.ID
  WHERE T1.ID = 666
) x
GROUP BY id, acct_no
于 2012-10-04T12:57:51.507 回答
2

你的语法无可挑剔。不幸的是,PIVOT 是在 Oracle 11g 中引入的,而您正在使用 10g。

于 2012-10-04T12:42:39.403 回答