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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:56:23+00:00 2026-06-02T20:56:23+00:00

If I have rows with this data: ID |Name |ContractType| —|————|————| 1 |Aaron Shatz

  • 0

If I have rows with this data:

ID |Name        |ContractType|
---|------------|------------|
 1 |Aaron Shatz | 6-month    |
 2 |Jim Smith   |12-month    |
 3 |Jim Smith   | 6-month    |
 4 |Mark Johnson|12-month    |

I can’t use Id to determine which record to use: I have to use ContractType. I want to select all records from a table, but if there are records with the same Name value, I want to pick the 12-month contract record.

The result of the query should be:

ID |Name        |ContractType|
---|------------|------------|
 1 |Aaron Shatz | 6-month    |
 2 |Jim Smith   |12-month    |
 4 |Mark Johnson|12-month    |
  • 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-02T20:56:25+00:00Added an answer on June 2, 2026 at 8:56 pm

    This works only because the OP has confirmed that only two contract types are possible, and the one he wants (for each contractor) happens to be the one that orders first alphabetically. So a couple of coincidences make this solution straight-forward.

    ;WITH x AS
    (
      SELECT ID, Name, ContractType, rn = ROW_NUMBER() OVER
        (PARTITION BY Name ORDER BY ContractType)
      FROM dbo.some_table
    )
    SELECT ID, Name, ContractType 
      FROM x
      WHERE rn = 1
      ORDER BY ID;
    

    If you need to make this more dynamic, I suppose you could say:

    DECLARE @PreferredContractType VARCHAR(32);
    SET @PreferredContractType = '12-month';
    
    ;WITH x AS
    (
      SELECT ID, Name, ContractType, rn = ROW_NUMBER() OVER
        (PARTITION BY Name ORDER BY CASE ContractType
          WHEN @PreferredContractType THEN 1 ELSE 2 END
        )
      FROM dbo.some_table
    )
    SELECT ID, Name, ContractType 
      FROM x
      WHERE rn = 1
      ORDER BY ID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some xml like this: <Data> <Rows> <Row> <Field Name=title>Mr</Field> <Field Name=surname>Doe</Field> <Row>
atm I have something like this: $('#fish').append(<div data-bind='foreach: rows'> + <div ... + <p>...</p>..);
I have rows like this... | NAME | RED | BLUE | GREEN |
Say I have a bunch of data on some people. This could include Name,
I have a bunch of name/email fields in my form like this: data[Friend][0][name] data[Friend][1][name]
This code works when I have data rows returned for the messages object but
I have this data. I need to get the lowest $ full rows for
I have this schema which I need to match 2 rows from user_data :
I have an HTML table containing a few rows (this is built dynamically). All
I have this table: id feed_id ... Let's say that I have 500 rows

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.