Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:33:13+00:00 2026-06-05T11:33:13+00:00

I would like to have paging & dynamic orderby clause in a stored procedure

  • 0

I would like to have paging & dynamic orderby clause in a stored procedure (THAT TOO ON MORE THAN ONE COLUMN). I did try following but gives me an error

It seems I cannot use Row_number() over rank()

Windowed functions cannot be used in the context of another windowed function or aggregate.

Is there any alternative to achieve this one, apart from linq to sql

SELECT  [t8].[AssetId], 
            [t8].[WorkOrderId], 
            [t8].[IssueDescription] AS [WorkOrderDescription], 
            [t8].[value] AS [Type], 
            [t8].[WorkOrderStatusTypeName] AS [Status],
            [t8].[value2] AS [StartDate], 
            [t8].[CompletedDate] AS [CompleteDate], 
            [t8].[value22] AS [CompletedBy], 
            ISNULL([t8].[value3],0) AS [Hours]
    FROM    (
                SELECT  ROW_NUMBER() 
                    OVER (ORDER BY  CASE WHEN @sortColumnName = 'default' THEN (RANK() over( order by [t7].[WorkOrderStatusTypeId] ASC, [t7].[WorkOrderId])) END ,
                                    CASE WHEN @sortColumnName = 'WorkOrderId' AND @sortOrder = 'asc' THEN [t7].[WorkOrderId] END ASC,
                                    CASE WHEN @sortColumnName = 'WorkOrderId' AND @sortOrder = 'desc' THEN [t7].[WorkOrderId] END DESC
                    ) AS [ROW_NUMBER],  
                    [t7].[AssetId], 
                    [t7].[WorkOrderId], 
                    [t7].[IssueDescription], 
                    [t7].[value], 
                    [t7].[WorkOrderStatusTypeName], 
                    [t7].[value2], 
                    [t7].[CompletedDate],
                    [t7].[value22], 
                    [t7].[value3]
            from    --Different tables      
            ) as t8     
    WHERE       [t8].[ROW_NUMBER] BETWEEN ((@pageIndex-1) * @pageSize)+ 1 AND @pageIndex * @pageSize
    ORDER BY    [t8].[ROW_NUMBER]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:33:14+00:00Added an answer on June 5, 2026 at 11:33 am

    Used

    SELECT  CASE    
            WHEN @sortColumnName ='default' AND @sortOrder = 'asc'  then row_number() over (order by [t7].[WorkOrderStatusTypeId], [t7].[CompletedDate] ASC) 
            WHEN @sortColumnName ='WorkOrderId' AND @sortOrder = 'asc'  then row_number() over (order by [t7].[WorkOrderId] ASC) 
            WHEN @sortColumnName ='WorkOrderId' AND @sortOrder = 'desc' then row_number() over (order by [t7].[WorkOrderId] DESC)
            END AS [ROW_NUMBER], 
    

    Instead of

    SELECT  ROW_NUMBER() 
            OVER (ORDER BY  CASE WHEN @sortColumnName = 'default' THEN (RANK() over( order by [t7].[WorkOrderStatusTypeId] ASC, [t7].[WorkOrderId])) END ,
                            CASE WHEN @sortColumnName = 'WorkOrderId' AND @sortOrder = 'asc' THEN [t7].[WorkOrderId] END ASC,
                            CASE WHEN @sortColumnName = 'WorkOrderId' AND @sortOrder = 'desc' THEN [t7].[WorkOrderId] END DESC
            ) AS [ROW_NUMBER],
    

    Cannot have nested windowed functions like RowNumber() and Rank() together, instead they can be used inside case statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have an AppWidget that designed like this one . Image:
I have developed a paging control that I would like to use in a
So in my project i would like have a nice treeview that has images.
I would like to have a more colorful Python prompt in the terminal, just
I would like to have a border that is 4px thick pink on the
I would like to have a custom view in my application that consists of
I have html output in paging section like this; <p>&nbsp;<strong class=active>1</strong>&nbsp;<a href=http://localhost/project/report_nc/search_now/1>2</a>&nbsp;<a href=http://localhost/project/report_nc/search_now/2>3</a>&nbsp;<a href=http://localhost/project/report_nc/search_now/1>Next
I would like to have one input field, where user will input search data.
I would like to have the cursor in the JavaDoc area when creating interfaces,
I would like to have a field which is updated on every change(insertion, modification),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.