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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:59:07+00:00 2026-05-11T18:59:07+00:00

this is a follow on question to a previously asked question . I have

  • 0

this is a follow on question to a previously asked question.

I have the following data in a single db table.

Name                LeftId    RightId
------------------------------------------
Cat                     1  
Cat                     1
Dog                     2
Dog                     2
Dog                               3
Dog                               3
Gerbil                  4         5 
Cat                
Bird
Cow                     6
Cow
Cow                               7
Dog                     8         9

Note that some rows have NO data for for LeftId and RightId.

Now, what i wish to do is find two different queries

  1. All rows which have at least 1 Id in one of the two columns AND row with NO data in both of those two Id columns.

eg.

Cat     1
Cow     6 (or 7 .. i'm not worried)
  1. All the rows where LeftId and RightId are NULL grouped by the same name. If another row (with the same name) has a value in the LeftId or RightId, this this name will not be returned.

eg.

Bird

hmm..

EDIT: Reworded the first question properly.

  • 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-11T18:59:07+00:00Added an answer on May 11, 2026 at 6:59 pm

    For the first query, you want rows that answer to both of the following criteria:

    1. The Name in the row appears in the table in the same row in which LeftId and RightId are both NULL.
    2. The Name in the row appears in the table in same row where at at least one of LeftId and RightId is not NULL.

    Well, #1 is done by:

    SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
    

    And #2 is done by:

    SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
    

    You could intersect them to see which Names appear in both lists:

    SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
    INTERSECT
    SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
    

    Which returns:

    Name
    ----
    Cat
    Cow
    

    But you want the LeftId and RightId, and you don’t care which, so I guess we’ll aggregate on the Name:

    SELECT Name, MIN(LeftId) AS LeftId, MIN(RightId) AS RightId 
        FROM Tbl WHERE Tbl.Name IN (
          SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
          INTERSECT
          SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
        )
    GROUP BY Name
    

    Which returns

    Name  LeftId  RightId
    ----  ------  -------
    Cat   1
    Cow   6       7
    

    lc already suggested using COALESE to turn those two IDs to a single one. So how about this:

    SELECT Name, COALESCE(MIN(LeftId),MIN(RightId)) AS Id 
        FROM Tbl WHERE Tbl.Name IN (
          SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
          INTERSECT
          SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
        )
    GROUP BY Name
    

    Which returns:

    Name  Id
    ----  --
    Cat   1
    Cow   6
    

    For the second query, you want rows that obey the following criteria:

    1. The Name appears only in rows that have no LeftId and RightId

    I can’t think of a way to do that sort of self-referencing query in SQL in a single set of criteria, so I’ll break it down to two criteria. Both must be obeyed to be acceptable:

    1. The Name appears in rows that have no LeftId and RightId
    2. The Name does not appear in rows that have either LeftId or RightId

    Doing #1 is simply:

    SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
    

    But #2 is tricky. Of course doing the opposite of #2 (“all the Name that appear in rows that have either LeftId or RightId) is just like before:

    SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
    

    Now comes the tricky bit – we want all the rows that obey #1 but don’t obey the opposite of #2. This is where EXCEPT is useful:

    SELECT Name FROM Tbl WHERE (LeftId IS NULL) AND (RightId IS NULL)
    EXCEPT
    SELECT Name FROM Tbl WHERE (LeftId IS NOT NULL) OR (RightId IS NOT NULL)
    

    Which returns:

    Name
    ----
    Bird
    

    Which is what we wanted!

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

Sidebar

Related Questions

Ok, I have previously asked a similar question to this but it was voted
This is a follow up question to a previous question I asked about calculating
This is a follow up to the question asked here: Groovy parsing text file
So this is a follow-up to a previous question that I asked: Trying to
So, I previously asked this question: Can someone help me compare using F# over
I realise this question has been asked before however the previous answers have gotten
I wanted to follow up on a question I previously asked which I'm still
This is a follow up to a previous question I asked How to encode
this is actually a follow up to a question I asked yesterday about problems
This is a follow up to my related question posted previously. My .NET application

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.