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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:36:45+00:00 2026-06-11T15:36:45+00:00

I had most of this query down until a new condition arose and it

  • 0

I had most of this query down until a new condition arose and it has confounded me. Given the following simplified table schema:

Parent Table:

ID  
FName  
LName 

Child Table:

[Index]  
ParentID  
Active_Flag  
ExpirationDate 

What I want to do is get Parent rows for which:

  1. There are no children.
  2. There are children whose Active_Flag is 1 but whose expiration dates are blank or NULL.
  3. There are indeed children but none have the Active_Flag set to 1.

The following query came up with my first two criteria:

SELECT p.ID, p.LNAME, p.FNAME,
    CASE 
        WHEN COUNT(ct.indx) = 0 THEN 'None'
        WHEN ct.ExpirationDate is NULL or ct.ExpirationDate = '' THEN 'No expiration date'
    END AS Issue
FROM ParentTable AS p 
    LEFT JOIN ChildTable ct
        ON p.ID = ct.ParentID
GROUP BY p.ID, p.LNAME, p.FNAME, ct.[INDEX], ct.ExpirationDate
HAVING (COUNT(ct.[INDEX]) = 0) OR (ct.ExpirationDate IS NULL OR ct.ExpirationDate = '')
ORDER BY p.LNAME

I don’t know how to account for #3. Any help is appreciated. Thanks in advance.

  • 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-06-11T15:36:46+00:00Added an answer on June 11, 2026 at 3:36 pm

    You can also do this in the HAVING clause:

    SELECT p.ID, p.LNAME, p.FNAME,
           (CASE WHEN COUNT(ct.indx) = 0 THEN 'None'
                 WHEN ct.ExpirationDate is NULL or ct.ExpirationDate = '' THEN 'No expiration date'
                 WHEN sum(case when ActiveFlag = 1 then 1 else 0 end) = 0 then 'No active children'
            END) AS Issue
    FROM ParentTable p LEFT JOIN
         ChildTable c
        ON p.ID = ct.ParentID
    GROUP BY p.ID, p.LNAME, p.FNAME
    HAVING (COUNT(ct.[INDEX]) = 0) OR
           (ct.ExpirationDate IS NULL OR ct.ExpirationDate = '') or
           sum(case when ActiveFlag = 1 then 1 else 0 end) = 0
    ORDER BY p.LNAME
    

    The DISTINCT in the SELECT is redundant. You do not need it with an aggregation.

    You can simplify the having to “sum(ActiveFlag)” if the activeFlag is indeed an integer. If not, then it should be “= ‘1’” rather than “= 1′.

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

Sidebar

Related Questions

I'm quite new to LINQ. Suppose that I had the following table: Incident ID
This might be the most stupid thing i've ever seen, i had a broken
Had this working; at one stage. The problem is the following text is now
So really my question is WHY this worked. Anyway, I had this query that
Has anyone had any luck with querying/changing SPNs on a Windows domain? Most of
I had to write a query to update all records in a table based
Imagine I had this table: declare @tmpResults table ( intItemId int, strTitle nvarchar(100), intWeight
I've had the thrill recently of developing web service applications. Most of my experience
I'm a C# developer for the most part. Back in college I had classes
I was using the following code to retrieve HTML snippets from a database table

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.