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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:43:50+00:00 2026-06-14T06:43:50+00:00

I have a table : TABLE A ID (PK)(Identity seed) Key (nvarchar) (Unique Hashed

  • 0

I have a table :

TABLE A

   ID (PK)(Identity seed)
   Key (nvarchar) (Unique Hashed Keys)

Data:

   ID          Key
   ----------- --------------------------------------------------
   1           ++0a4rZicJ68kProEpK/ig                            
   2           ++0Coy1S7szg3NjLi2kMLQ                            
   3           ++0eeYiZuRPxsiVVsBcfdg                            
   4           +F07I6n6kLvC/I98So8Y+w                            
   5           +f/RK7VMWIIo5IfUcMujmg  

TABLE B : (Without PK)

   Content (nvarchar)
   SortKey (nvarchar) 

Data :

   Content          SortKey 
   ----------- --------------------------------------------------
   TEST1           ++0a4rZicJ68kProEpK/ig                            
   TEST2           ++0Coy1S7szg3NjLi2kMLQ                            
   TEST3           ++0eeYiZuRPxsiVVsBcfdg                            
   TEST4           +F/ZdeGRjbC4sP6ulQnOvg                            
   TEST5           +f0+6vJcwY++Xdx5lch1kQ 
   TEST6           +f/RK7VMWIIo5IfUcMujmg <-- Expected Result starts here
   TEST7           +F07I6n6kLvC/I98So8Y+w
   TEST8           +f0990bHYJUOXkyME+0kmg   

Query :

SELECT top 3 * 
FROM 
    TABLEB 
WHERE 
    SortKey > (SELECT top 1 Key 
               FROM TABLEA 
               ORDER BY ID DESC)
ORDER BY 
    SortKey 

The above query gives me the desired result which is greater than the SortKey (++1l32JdpYoHzXTCIp4jSA):

TEST6           +f/RK7VMWIIo5IfUcMujmg 
TEST7           +F07I6n6kLvC/I98So8Y+w
TEST8           +f0990bHYJUOXkyME+0kmg 

Once I get this result I need to get the final (last record) key again to do an insert on another table, so I need to get the key of TEST8 in this scenario i.e. : f/RK7VMWIIo5IfUcMujmg.

How do I do this in a single query and not have to write 2 separate queries?

  • 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-14T06:43:51+00:00Added an answer on June 14, 2026 at 6:43 am

    You could use ROW_NUMBER()

    WITH CTE AS 
    (
    SELECT
          ROW_NUMBER() OVER (ORDER BY CONTENT DESC) rn,
          Content,
          SortKey
    FROM   tableb 
    WHERE  sortkey < (SELECT TOP 1 [key] 
                      FROM   tablea 
                      ORDER  BY id DESC) 
     )
    SELECT 
         Content,
         SortKey 
    FROM CTE 
    WHERE RN = 1
    

    DEMO

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

Sidebar

Related Questions

Let's say I have a table my_table(id int identity(1,1) not null primary key, data
So I have a table with an identity column as the primary key, so
OK, I have a table with no natural key, only an integer identity column
I have a table with a IDENTITY Column as Primary Key (a classic ID
I have a table with an Identity column as Primary Key. All is well
I have a table CREATE TABLE [dbo].[MergeAndDeleteNonprofitDetails]( ID int IDENTITY(1,1) PRIMARY KEY, [ToJKNonprofitID] [int]
I have a table with two very important fields: id INT identity(1,1) PRIMARY KEY
In Sql Server, I have a table with an Identity primary key. Often I
I have a table with an identity column that have incremented by 1 for
I have a table with following columns: ContractorId ......... INT ............. IDENTITY ContractorName ........

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.