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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:43:44+00:00 2026-05-23T13:43:44+00:00

I have a table with the following simplified data: PK FK Type —————- 1

  • 0

I have a table with the following simplified data:

PK    FK   Type
----------------
1     11    A
2     11    B
3     12    B
4     13    C
5     13    D
6     14    D

And I want a result set of:

PK    FK   Type
---------------
1     11    A
3     12    B
4     13    C
6     14    D

So if a given FK value has a Type A, don’t give me any rows with B, C, or D. Or if the value has a Type B, then filter out C and D, etc. This feels like I need to apply a window function or coalesce, but I’m not sure how to do it.

  • 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-23T13:43:45+00:00Added an answer on May 23, 2026 at 1:43 pm

    Use:

    SELECT x.* 
      FROM (SELECT s.*, 
                   ROW_NUMBER() OVER(PARTITION BY s.fk
                                         ORDER BY s.pk) rnk
              FROM YOUR_TABLE s) x
     WHERE x.rnk = 1
    

    …or using CTE (no performance difference):

    WITH example AS (
      SELECT s.*, 
             ROW_NUMBER() OVER(PARTITION BY s.fk
                                   ORDER BY s.pk) rnk
        FROM sample s)
    SELECT x.* 
      FROM example x
     WHERE x.rnk = 1
    

    Proof:

    WITH sample AS (
      SELECT 1 AS PK, 11 AS FK, 'A' AS [TYPE]
      UNION ALL
      SELECT 2 AS PK, 11 AS FK, 'B' AS [TYPE]
      UNION ALL
      SELECT 3 AS PK, 12 AS FK, 'B' AS [TYPE]
      UNION ALL
      SELECT 4 AS PK, 13 AS FK, 'C' AS [TYPE]
      UNION ALL
      SELECT 5 AS PK, 13 AS FK, 'D' AS [TYPE]
      UNION ALL
      SELECT 6 AS PK, 14 AS FK, 'D' AS [TYPE])
    SELECT x.* 
      FROM (SELECT s.*, ROW_NUMBER() OVER(PARTITION BY s.fk
                                              ORDER BY s.pk) rnk
              FROM sample s) x
     WHERE x.rnk = 1
    

    Result:

    pk   fk   type
    ----------------
    1    11   A
    3    12   B
    4    13   C
    6    14   D
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]
I have a several tables nested within a table that I am parsing using
I have the following situation (SQL Server Express): 2 tables connected via a pk-fk
I have the following insert stored procedure: CREATE Procedure dbo.APPL_ServerEnvironmentInsert ( @ServerEnvironmentName varchar(50), @ServerEnvironmentDescription
I've got a report that has been in use quite a while - in
I have to search in two tables that are in a parent-child relation (1:n)
Let me illustrate this question with a simplified example. Assume I am building a
I need to update some rows in a DB table. How I identify the
I hope anyone can help. I have to develop up against this third party
It would seem that there is a much simpler way to state the problem.

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.