2

我正在尝试执行此代码,但它说我在row_number()表达式中遗漏了一些我根本无法弄清楚的东西。

with summary as 
(select s.city, length(s.city) as C_length, 
row_number() over (partition by length(s.city), order by s.city)
as r1 from station s
where 
length(s.city) = (SELECT min(LENGTH(s1.CITY)) FROM STATION s1)
or length(s.city) = (SELECT max(LENGTH(s2.CITY)) FROM STATION s2))
select su.city, su.C_length 
from summary su;
4

2 回答 2

2

partition by子句和子句之间不应该有逗号order by。只需将其删除,您应该就可以了:

row_number() over (partition by length(s.city) order by s.city)
-- Comma removed here ------------------------^
于 2017-02-22T20:24:36.337 回答
1

您的查询可以简化:

with summary as (
      select s.city, length(s.city) as C_length, 
             min(length(s.city)) over () as min_length,
             max(length(s.city)) over () as max_length,
      from station s
     )
select su.city, su.C_length 
from summary su
where c_length in (min_length, max_length);

我删除r1了,因为它没有被使用。

于 2017-02-22T20:31:13.603 回答