我正在尝试在H2中对重复行进行排名。由于H2不支持像RANK()
这样的窗函数。我正在尝试使用Row_Number()
,但它不起作用会抛出错误说法语错误。请指导有什么问题。我是SQL和H2数据库的新手。
Insert into t2(c1, c2, c3, rank1, rank2)
select
c1, c2, c3,
ROW_NUMBER OVER(partition by c1 order by c3) as rank1,
ROW_NUMBER OVER(partition by c2 order by c3) as rank2
from t1
请指导。提前致谢。如何解决上述语法错误?它说 ; )预期。另外,请让我知道好的解决方法,以便在H2数据库中找到排名。
答案 0 :(得分:2)
尝试用带有COUNT()的子查询替换ROW_NUMBER()。唯一的问题是他们将为相同的C3
字段生成相同的排名。
select
c1, c2, c3,
(SELECT COUNT(*) FROM T1 as T3 WHERE T3.C1=T1.C1 AND T3.C3<=T1.C3 ) as rank1,
(SELECT COUNT(*) FROM T1 as T3 WHERE T3.C2=T1.C2 AND T3.C3<=T1.C3 ) as rank2
from t1