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

  • Home
  • SEARCH
  • 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 7707801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:27:07+00:00 2026-06-01T00:27:07+00:00

I have a SQL Server 2008 database where I would like to be able

  • 0

I have a SQL Server 2008 database where I would like to be able to do the following on one table.

The table has multiple columns that should be unique based on a combination of two columns.
We will call them [ID1] and [ID2], then there is a key we will call [index] and a value that could be duplicated called [ID3] and a datetime value called [start].

So here is the dilemma, within the scope of the table there are should only be one increasing value of [index] for every [ID1 ]and [ID2] combination, the three form a natural PK from the client DB that is being warehoused in one consolidated server database.
ID3 is indicative of a value that was used to determine when a row was stored in the client DB so there may be duplicates in the server

[ID1]   [ID2]   [index]     [ID3]   [start]         [other1]    [other2]
1   1   1       1   01/01/2000 01:00:00 5       6
1   1   2       2   01/01/2000 01:00:01 4       2
1   1   3       3   01/01/2000 01:00:02 5       2
1   1   4       3   01/01/2000 01:00:03 5       2
1   1   5       4   01/01/2000 01:00:04 4       6

What I want is a query that will return rows that are unique combinations of [ID3] and the [other1] & [other2] columns, unique to the [ID1], [ID2] key, with that I would like the first [start] that fits that criteria, essentially ignoring further occurrences of the same distinct clause.

From the above table it would return …

[ID1]   [ID2]   [index]     [ID3]   [start]         [other1]    [other2]
1   1   1       1   01/01/2000 01:00:00 5       6
1   1   2       2   01/01/2000 01:00:01 4       2
1   1   3       3   01/01/2000 01:00:02 5       2
1   1   5       4   01/01/2000 01:00:04 4       6

The second row with the [ID3] of the value 3 would be ignored, as would any other that had duplicates of [ID3]

The point I cannot seem to get to is the first value of each distinct combination, because the distinct does not allow me to select the value of the other columns, a group by would require some aggregate function.

  • 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-01T00:27:09+00:00Added an answer on June 1, 2026 at 12:27 am
    ;with a as
    (
    SELECT [ID1],[ID2],[index],[ID3],[start],[other1],[other2], 
    rn = row_number() over (partition by ID1, ID2, ID3 order by start)
    FROM yourtable
    )
    select 
    [ID1],[ID2],[index],[ID3],[start],[other1],[other2], 
    from a
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in my SQL Server 2008 R2 database, and would like
I have a SQL Server 2008 database with a table that has a column
i have multiple users connecting to sql-server-2008 database. i would like to know whether
I have a table in a sql server 2008 database that contains bunch of
I have a large (~ 40gb ) SQL Server 2008 database that I would
I'm helping implement a SQL Server 2008 database and I have several columns that
I have a SQL Server 2008 database with records like the following 001 CAT
I have an application that accesses a sql server 2008 database. The server and
A have a database on MS-SQL Server 2008 that is growing a lot, year
I have a database that I have created using SQL Server Developer 2008. I

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.