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 653667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:24:28+00:00 2026-05-13T22:24:28+00:00

I want to query a table so that it´s ordered the following way: 1)

  • 0

I want to query a table so that it´s ordered the following way:

1) “entry”
2) “entry#”
3) “entry something”
4) “..entry..”

I did this via Union All and 4 different queries.

But additionally, I want to include paging, so that I can f.ex. receive the row-numbers 1-100, 101-200 etc. I tried Row_Num() Over (Order By) but did not get it done, I think because the Order By-clause destroys my
own order.

My query looks something like this (a little bit simplified)

SELECT Keyword FROM Keyword WHERE Keyword LIKE 'Keyword' Union All SELECT * FROM Keyword WHERE Keyword like '%Keyword%' AND NOT LIKE 'Keyword'

and the whole query i tried out:

WITH SearchResult as 
(SELECT * FROM Keyword WHERE Keyword like 'Keyword' Union All SELECT * FROM Keyword WHERE Keyword like '%Keyword%' AND NOT LIKE 'Keyword') 
SELECT * FROM 
(SELECT ROW_NUMBER() OVER (ORDER BY Keyword DESC) AS RowNum, * 
 FROM SearchResult) AS Results 
WHERE (RowNum BETWEEN (1 - 1) * 100 + 1 AND 1 * 100)
  • 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-05-13T22:24:28+00:00Added an answer on May 13, 2026 at 10:24 pm

    Wrap your unioned queries in another one as a derived table and you can use the top clause.

    SELECT TOP 100 * FROM (
       SELECT * FROM table where field = 'entry'
       UNION ALL
       SELECT * FROM table where field = 'entry#'
    ) sortedresults
    

    You were on the right track then. Add a defined column to each of your subsets of sorted results and then you can use that to keep the order sorted.

    WITH SearchResult AS
      (SELECT *, ROW_NUMBER() OVER (ORDER BY QueryNum) as RowNum FROM
         (SELECT *, 1 as QueryNum FROM KeywordTable WHERE field = 'Keyword'
          UNION ALL
          SELECT *, 2 from KeywordTable WHERE field = 'Keyword#'
          ) SortedResults
      )
    SELECT * from SearchResults WHERE RowNum BETWEEN 4 and 10
    

    It is important that you also sort each subquery by something other than keyword so their order stays the same between runs (and as a secondary sort on the row number function). Example: say you have k1, k2, k3, k4, k5 – if you select * where keyword like k% you might get k1, k2, k3, k4, k5 one time and k5, k4, k3, k2, k1 the next (SQL doesn’t guarantee return order and it can differ). That will throw off your paging.

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

Sidebar

Related Questions

I have a query that does what i want joining table but i need
I want to query the name of all columns of a table. I found
I want to write a query to select from a table all rows with
I have a table by the following structure and records, All i want to
I have a MySQL query that produces this output ordered like this: Instead I'd
I want a Codeigniter select query from a table with three conditions in .
I want to use Access database with query for pivot table in Excel. How
I want to print some dynamic query to execute a procedure on all tables
I have a table that looks like this one : +------+------+------------------+ | item |
I have the following coding for a button. My problem is that the Query

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.