无法为db2

时间:2016-03-07 13:27:53

标签: sql db2

我无法为涉及row_number()的查询创建视图。

例如,我有一个包含一列Id的样本表。当我尝试以下列方式创建视图时,它会出错。

create or replace view sample_view
(
    ID
)

AS
select Id from (
    select Id,row_number() over (partition  by id) as rownum
    from sample
    where rownum =1 )
  

错误消息:没有类型名为“ROW_NUMBER”的授权例程   找到具有兼容参数的“FUNCTION”.. SQLCODE = -440,   SQLSTATE = 42884,DRIVER = 4.16.53

不确定为什么在创建视图时会抛出此错误,但是当我尝试运行此查询时:

 select Id from (
    select Id,row_number() over (partition  by id) as rownum
    from sample
    where rownum =1 )

它已成功执行。

1 个答案:

答案 0 :(得分:1)

~/.bashrc位于外部查询中,而不是子查询:

where

我假设你有更复杂的代码,因为这个查询更容易写成:

select Id
from (select Id, row_number() over (partition by id order by <something goes here>) as rownum
      from sample
     ) s
where rownum = 1;

编辑:

DB2还支持select distinct Id from sample; ,这可能更简单:

fetch first 1 row only