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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:01:02+00:00 2026-05-28T15:01:02+00:00

One of the columns in my table is DataSource. Datasource can have two values,

  • 0

One of the columns in my table is “DataSource”. Datasource can have two values, lets say “A” or “B”. I would like to always take the row when Datasource = “A”, however, if there isn’t an entry for A I would like to take “B”.

How does one do this in SQL Server?

EDIT:

So for a partucular product (Product ID) one Product ID may have two rows, each containing a different DataSource, wheres another ProductID may only have one DataSource:

{ProductID DataSource}
{1  A},
{1  B},
{2  B}

Here I would wish to select the top and bottom row

  • 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-28T15:01:03+00:00Added an answer on May 28, 2026 at 3:01 pm

    Based on your sample data, this works:

    SELECT ProductID, MIN(DataSource)
    FROM @tab
    GROUP BY ProductID;
    

    And here is my test data:

    declare @tab table (ProductID int, DataSource char(1))
    insert into @tab values (1, 'A');
    insert into @tab values (1, 'B');
    insert into @tab values (2, 'B');
    

    If there are more columns in the table than the two you show then:

    SELECT T1.*
    FROM @tab T1
    JOIN
    (
        SELECT ProductID, MIN(DataSource) AS DataSource
        FROM @tab
        GROUP BY ProductID
    ) T2 ON T1.ProductID = T2.ProductID AND T1.DataSource  = T2.DataSource
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with two columns and one row, and 100% width across
Is it a bad thing to have two xml columns in one table? +
I have a table and one of the columns holds web addresses like: 'http://...'
I have asp:datalist binding from table this table contain two columns one of them
I have an RDLC report and would like one of the columns in a
How would one get columns information on table which he doesn't own, but has
I have a table that containts a set of columns one of it is
I have one database table which contains 8 columns. One of the columns is
I have a table with few columns and one of the column is DockNumber.
I have a table where one column has duplicate records but other columns are

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.