Oracle SQL - SCD跟踪随时间变化的属性

时间:2015-01-24 18:39:09

标签: sql oracle scd

我遇到以下错误:

ORA-30483: window  functions are not allowed here
30483. 00000 -  "window  functions are not allowed here"

我试图通过时间构建BOOK_NO和WALK_NO属性更改的水平视图。

我认为一个合理的方法是RANK the effective_to日期,只需解码该值的book_no || walk_no属性....但不确定从这里开始的地方!

下面的SQL:

Select METER_PIT_REF 
,MAX(DECODE( RANK() OVER (PARTITION BY METER_PIT_REF ORDER BY EFFECTIVE_TO DESC),1,BOOK_NO||WALK_NO) )
,MAX(DECODE( RANK() OVER (PARTITION BY METER_PIT_REF ORDER BY EFFECTIVE_TO DESC),2,BOOK_NO||WALK_NO) )
,MAX DECODE( RANK() OVER (PARTITION BY METER_PIT_REF ORDER BY EFFECTIVE_TO DESC),3,BOOK_NO||WALK_NO) )
FROM BW_PIT_BOOKWALK_BRIDGE
WHERE BOOK_NO = 'M04'
GROUP BY METER_PIT_REF

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:2)

使用子查询:

Select METER_PIT_REF,
       MAX(CASE WHEN r = 1 THEN BOOK_NO||WALK_NO END),
       MAX(CASE WHEN r = 2 THEN BOOK_NO||WALK_NO END),
       MAX(CASE WHEN r = 3 THEN BOOK_NO||WALK_NO END)
FROM (SELECT b.*,
             RANK() OVER (PARTITION BY METER_PIT_REF ORDER BY EFFECTIVE_TO DESC) as r
      FROM BW_PIT_BOOKWALK_BRIDGE
     ) b
WHERE BOOK_NO = 'M04'
GROUP BY METER_PIT_REF;

我还将DECODE()替换为CASE。它是ANSI标准的等价物。