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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:55:57+00:00 2026-05-28T19:55:57+00:00

I need a select query .. Environment : SQL DBA -SQL SERVER 2005 or

  • 0

I need a select query ..

Environment : SQL DBA -SQL SERVER 2005 or newer

Example :
In this sample table, if I select top 20 no duplicate records should come and next record should be in 20 records .

Example :

123456 should not repeat in 20 records and if 18th is duplicate, in place of 18th, 19th record should come and in 19th—20th should come, in 20th —21st should come .

No concern of Asc or Desc for rows .

Lookup Table before

Id                                  Name 
123456                              hello
123456                              hello
123654                              hi
123655                              yes

LookUp Table after

Id                                  Name
123456                              hello
123654                              hi
123655                              yes

My table:

CREATE TABLE [dbo].[test](  
[Id] [int] IDENTITY(1,1) NOT NULL,
[ContestId] [int] NOT NULL,
[PrizeId] [int] NOT NULL,
[ContestParticipantId] [int] NOT NULL,
[SubsidiaryAnswer] [varchar](256) NOT NULL,
[SubsidiaryDifference] [bigint] NOT NULL,
[AttemptTime] [datetime] NOT NULL,
[ParticipantName] [varchar](250) NOT NULL,
[IsSubscribed] [bit] NOT NULL,
[IsNewlyRegistered] [bit] NOT NULL,
[IsWinner] [bit] NOT NULL,
[IsWinnerConfirmed] [bit] NOT NULL,
[IsWinnerExcluded] [bit] NOT NULL) ON [PRIMARY] 

My question is: from this select, we actually need the first 20, but unique ones.

SELECT TOP 20 * FROM test order by SubsidiaryDifference 

When we do the above query, we have currently some double in there. In case there is a double, we need take them only 1 time and take the next one

Any one know this issue ?

Thanks in advance 🙂

  • 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-28T19:55:57+00:00Added an answer on May 28, 2026 at 7:55 pm

    Reading your question, it appears you don’t really want to delete the rows from the table – you just want to display the TOP 20 distinct rows – you try something like this:

    ;WITH LastPerContestParticipantId AS
    (
       SELECT 
           ContestParticipantId,
           -- add whatever other columns you want to select here
           ROW_NUMBER() OVER(PARTITION BY ContestParticipantId
                             ORDER BY SubsidiaryDifference) AS 'RowNum'
       FROM dbo.Test
    )
    SELECT TOP (20) 
        ContestParticipantId, 
        -- add whatever other columns you want to select here
        SubsidiaryDifference
    FROM 
        LastPerContestParticipantId
    WHERE 
        RowNum = 1
    

    This will show you the most recent row for each distinct ContestParticipantId, order by SubsidiaryDifference – try it!

    Update #2: I’ve created a quick sample – it uses the data from your original post – plus an additional SubID column so that I can order rows of the same ID by something…

    When I run this with my CTE query, I do get only one entry for each ID – so what exactly is “not working” for you?

    DECLARE @test TABLE (ID INT, EntryName VARCHAR(50), SubID INT)
    
    INSERT INTO @test 
    VALUES(123456, 'hello', 1), (123456, 'hello', 2), (123654, 'hi', 1), (123655, 'yes', 3)
    
    ;WITH LastPerId AS
    (
       SELECT 
           ID, EntryName,
           ROW_NUMBER() OVER(PARTITION BY ID ORDER BY SubID DESC) AS 'RowNum'
       FROM @test
    )
    SELECT TOP (3) 
        ID, EntryName
    FROM 
        LastPerId
    WHERE 
        RowNum = 1
    

    Gives an output of:

    ID      EntryName
    123456   hello
    123654   hi
    123655   yes
    

    No duplicates.

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

Sidebar

Related Questions

I need a solution for a select query in Sql Server 2005. I'd like
I need to execute a SQL Query between two databases. Example: SELECT * from
I need to do a query like this: SELECT wposts.ID FROM posts wposts WHERE
I need to make a query like this: SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId
I need some help with SQL Query. I am trying to select all records
I need to write a select query that joins the tables based on a
Suppose we need to check three boolean conditions to perform a select query. Let
Suppose a query select * from employee returns 80 rows. I need to display
I have a query: SELECT ID FROM requests WHERE date <operator> <expression> I need
Database has tables Photos and PhotoAlbums. I need a query that will select all

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.