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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:17+00:00 2026-05-13T14:54:17+00:00

In a scenario where I have a table like so: int id (PK) int

  • 0

In a scenario where I have a table like so:

int id (PK)
int staff_id
int skill_id
bit mainskill

I want to select only ONE record for each staff member (represented by staff_id) listing their main skill as represented by a (1) in mainskill. If no main skill is present, I want to return any of the skill records for that staff member. For example:

id   staff_id   skill_id   mainskill
1    1          24         1
2    1          55         0
3    1          7          0
4    4          24         0
5    4          18         0
6    6          3          0
7    6          18         1

The query should return:

id   staff_id   skill_id   mainskill
1    1          24         1
4    4          24         0
7    6          18         1

I’ve tried various combinations of grouping, DISTINCT etc but can’t get the output I’m after. Any help appreciated.

  • 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-13T14:54:17+00:00Added an answer on May 13, 2026 at 2:54 pm

    SQL Server 2005+, Using CTE:


    WITH rows AS (
      SELECT t.id,
             t.staff_id,
             t.skill_id,
             t.mainskill,
             ROW_NUMBER() OVER (PARTITION BY t.staff_id ORDER BY t.mainskill DESC) AS rank
        FROM TABLE t)
      SELECT r.id,
             r.staff_id,
             r.skill_id,
             r.mainskill
        FROM rows r
       WHERE r.rank = 1
    ORDER BY r.staff_id
    

    SQL Server 2005+, Non-CTE Equivalent:


      SELECT r.id,
             r.staff_id,
             r.skill_id,
             r.mainskill
        FROM (SELECT t.id,
                     t.staff_id,
                     t.skill_id,
                     t.mainskill,
                     ROW_NUMBER() OVER (PARTITION BY t.staff_id ORDER BY t.mainskill DESC) AS rank
                FROM TABLE t) r
       WHERE r.rank = 1
    ORDER BY r.staff_id
    

    Both use ROW_NUMBER, which is only available since SQL Server 2005.

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

Sidebar

Related Questions

I have a scenario outline table that looks like the following: Scenario Outline: Verify
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
My scenario is this: I have a table with a structure like this (simplified)
The architecture for this scenario is as follows: I have a table of items
I have a scenario where I have to update multiple rows in a table
I have the following scenario: a table of projects and a table of persons,
I have a following scenario in my SQL Server 2005 database. zipcodes table has
Here's the scenario: I have 2 tables with data, one is the 2009 version
Scenario: I have a fairly generic table (Data), that has an identity column. The

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.