ROW_NUMBER()在SQL中没有结束

时间:2012-10-16 12:15:42

标签: database sql-server-2005

有没有办法在SQL中使用ROW_NUMBER()而不使用OVER,因为我想使用排序。

我有一个带有多个可排序列的Grid,可配置行。在我的方案中,按顺序变量,这就是为什么我无法通过使用ROWNUM来下订单。

3 个答案:

答案 0 :(得分:43)

select ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as number from Task order by RuleId

答案 1 :(得分:0)

我用不同的逻辑来解决这个问题。我创建了一个表,该表仅列出工作日,然后采用第n个值,而不是检查每一天并确认它是工作日还是假日。

CREATE OR REPLACE FUNCTION add_n_working_days ( 
  start_date DATE, working_days PLS_INTEGER
) RETURN DATE AS
  l_end_date DATE := start_date;
  l_counter  pls_integer := 0;
BEGIN
  SELECT 
    business_day 
    INTO l_end_date
  FROM 
  (
    WITH 
    dates AS
      (SELECT start_date + level - 1  as dt FROM dual CONNECT BY level < 100)
    ,weekdates AS
    (SELECT dt as weekday FROM dates WHERE TO_CHAR(dt,'fmdy') NOT IN ('sat','sun'))
    ,business_days AS
    (
    SELECT weekday as business_day FROM weekdates
    MINUS
    SELECT holiday FROM so_holidays 
    )
    SELECT business_day, ROW_NUMBER() OVER (ORDER BY 1) as rn from business_days
  )
  WHERE rn = working_days + 1;
  RETURN l_end_date;
END add_n_working_days;

如果working_days太高,那么我建议将该值100设置为变量。

答案 2 :(得分:0)

select
a.City,
a.len
from
(
select city,length(city) AS len,
row_number()over(order by length(city) desc, city) as hi,
row_number()over(order by length(city), city) as lo
from
STATION) as a where (a.lo=1 or a.hi=1);