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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:03:50+00:00 2026-05-18T09:03:50+00:00

I ran into the following problem. I have a table like this: ID ID1

  • 0

I ran into the following problem.
I have a table like this:

ID   ID1     ID2     ID3     ID4     ID5
1   NULL    NULL    NULL    NULL    1
2   NULL    NULL    NULL    2       NULL
3   NULL    NULL    NULL    2       1
4   3       NULL    NULL    2       NULL
5   3       NULL    NULL    2       1
6   NULL    5       NULL    2       NULL

And I need to get distinct rows it terms that NULL equals any value. For this example the answer is:

ID   ID1     ID2     ID3     ID4     ID5
5   3       NULL    NULL    2       1
6   NULL    5       NULL    2       NULL

P.S. Here ID is primary key hence unique. ID1-ID5 – any integers.
Thanks in advance!
UPDATED
Saying that null equals any number I mean that it’s absorbed by any number.

  • 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-18T09:03:50+00:00Added an answer on May 18, 2026 at 9:03 am

    This works, don’t know if it can be made any simpler

    SELECT ID1, ID2, ID3, ID4, ID5
    FROM IDS OUTT
    WHERE NOT EXISTS (SELECT 1
                    FROM IDS INN
                    WHERE OUTT.ID != INN.ID AND
                          (ISNULL(OUTT.ID1, INN.ID1) = INN.ID1 OR (INN.ID1 IS NULL AND OUTT.ID1 IS NULL)) AND
                          (ISNULL(OUTT.ID2, INN.ID2) = INN.ID2 OR (INN.ID2 IS NULL AND OUTT.ID2 IS NULL)) AND
                          (ISNULL(OUTT.ID3, INN.ID3) = INN.ID3 OR (INN.ID3 IS NULL AND OUTT.ID3 IS NULL)) AND
                          (ISNULL(OUTT.ID4, INN.ID4) = INN.ID4 OR (INN.ID4 IS NULL AND OUTT.ID4 IS NULL)) AND
                          (ISNULL(OUTT.ID5, INN.ID5) = INN.ID5 OR (INN.ID5 IS NULL AND OUTT.ID5 IS NULL)))
    

    EDIT: Found a sweeter alternative, if your ids never have negative numbers

    SELECT ID1, ID2, ID3, ID4, ID5
    FROM IDS OUTT
    WHERE NOT EXISTS (SELECT 1
                    FROM IDS INN
                    WHERE OUTT.ID != INN.ID AND
                          coalesce(OUTT.ID1, INN.ID1,-1) = isnull(INN.ID1,-1) AND
                          coalesce(OUTT.ID2, INN.ID2,-1) = isnull(INN.ID2,-1) AND
                          coalesce(OUTT.ID3, INN.ID3,-1) = isnull(INN.ID3,-1) AND
                          coalesce(OUTT.ID4, INN.ID4,-1) = isnull(INN.ID4,-1) AND
                          coalesce(OUTT.ID5, INN.ID5,-1) = isnull(INN.ID5,-1))  
    

    EDIT2: There is one case where it won’t work – in case two rows (with different ids) have exact same form. I am assuming that it is not there. If such a thing is present, then first create a view with a select distinct on the base table first, and then apply this query.

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

Sidebar

Related Questions

I ran into the following problem. I have an xml-file like this: <Source Name
Ok, so I just ran into the following problem that raised an eyebrow. For
I've ran into a problem while trying to test following IRepository based on NHibernate:
While building my application with relational tables I ran into the following problem :
I ran into the following problem in dynamically creating topics for our ActiveMQ system:
I am writing my first MSBuild script and ran into a problem. I have
I have recently ran into this strange issue, I was trying to reference parent
Trying to make a MySQL-based application support MS SQL, I ran into the following
I ran into the problem that my primary key sequence is not in sync
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,

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.