Aya Mohammad
Aya Mohammad

Reputation: 245

Descending ordering doesn't work

This stored procedure doesn't perform descending order of data I don't know why !

BEGIN 
    SET nocount ON; 

    WITH ordereddata 
         AS (SELECT *, 
                    rn = Row_number() 
                           OVER ( 
                             ORDER BY articleid ) 
             FROM   articles) 
    SELECT * 
    FROM   ordereddata 
    WHERE  rn <= @UpperBound 
           AND rn >= @LowerBound 
    ORDER  BY articleid DESC 
END 

this is the result when I give the @Upperbound and @LowerBound values

aricleid 54 55 56 57 . . .

Upvotes: 1

Views: 64

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try like this

BEGIN
SET NOCOUNT ON;
WITH OrderedData
as (
  select * , rn = ROW_NUMBER() OVER (ORDER BY articleid desc )  FROM articles
   )

select * from OrderedData
where rn <= @UpperBound AND rn >= @LowerBound 
ORDER BY articleid desc
END

Upvotes: 1

Amit
Amit

Reputation: 15387

Try this

BEGIN
  SET NOCOUNT ON;
WITH OrderedData
as (
  select * , rn = ROW_NUMBER() OVER (ORDER BY articleid)  FROM articles 
  )

select * from OrderedData
where rn <= @UpperBound AND rn >= @LowerBound 
ORDER BY articleid desc
END

Upvotes: 1

Related Questions