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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:46:53+00:00 2026-05-31T20:46:53+00:00

Here is the sample data for test table I have [childId] [parentId] [present] 1

  • 0

Here is the sample data for test table I have

[childId] [parentId] [present]
1         10         0
2         11         1
3         NULL       0
4         NULL       0
NULL      10         0

Now, for all the parentIds (including those with NULL) I want to count all the present childIds

select parentId, count(childId) as nbr
from TestTable
where present=1 or parentId is NULL
group by parentId

The result I get is

parentId    nbr
NULL        2
11          1

Same count number (nbr) I get for both present=1 and present=0. It seems that I cannot impose a condition for the NULL grouped value.
What’s the mistake I’m making and what’s the solution for the query I want to make?


UPDATE:

Since the test case I gave is not representative, until I recreate the real situation in better way, I’ll try to explain the problem I’m facing.

I created a view with full outer join, so I have some records having NULL for childId and some records having NULL for parentId.
Now, I want to list all of the parentId values (both NULL and non NULL) and for each of them, count all the records that, for example, have present=1.
If I use where present=1 condition, it eliminates NULL values from result set, i.e. I won’t have result record NULL|x for parentId|nbr.
I solved this with union all. In first select I have where present=1 and in second select I have where present=1 and parentId is NULL.

select parentId, count(childId) as nbr
from TestTable
where present=0
group by parentId

union all

select parentId, count(childId) as nbr
from TestTable
where present=0 and parentId is NULL
group by parentId

The result I get is

    [parentId]  [nbr]
    NULL        2
    10          1
    NULL        2

The only problem with this (besides duplicated record for parentId=NULL), is that if there are no records with parentId=NULL and present=1, in result I won’t have record with NULL | 0 for parentId|nbr and I want to list ALL parentIds, both NULL and not NULL.
So to sum it up I need to have this format of output for present=1

    [parentId]  [nbr]
    NULL        0
    10          0
    11          1

and this one for present=0

    [parentId]  [nbr]
    NULL        2
    10          2
    11          0

Any help?

  • 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-31T20:46:54+00:00Added an answer on May 31, 2026 at 8:46 pm
    where present=1 or parentId is NULL
    

    That would be it. Either you have Present = 1 or parentID is null. There’s no clause saying that parentID should be null and present should be 1.

    What exactly are you trying to accomplish?

    To list all non-null parentID that has present = 1 and all null parentID regardless of present, you can use your where clause, but edit your group by to GROUP BY parentid, present. This way you will show all non-null with present 1, and all null, both present 0 and 1.

    UPDATE:
    Based on your new requirements, you need to also group for non-existing combinations. Nowhere in your dataset is there a NULL, 1 value – so your ordinary way of looking at it won’t make much sense. You need to separate the parentid and present from eachother.

    One way to do this is to simply do the following:

    SELECT parentID, Pres1.nbr_pres_1, Pres0.nbr_pres_0
    FROM t
    OUTER APPLY (SELECT COUNT(1) as nbr_pres_1 FROM t t1 WHERE 
                 coalesce( t.parentid , -999) = coalesce(t1.parentid ,-999) and present=1 ) Pres1
    OUTER APPLY (SELECT COUNT(1) as nbr_pres_0 FROM t t0 WHERE 
                 coalesce( t.parentid , -999) = coalesce(t0.parentid ,-999) and present=0 ) Pres0
    GROUP BY t.ParentID, Pres1.nbr_pres_1, Pres0.nbr_pres_0
    

    This is based on testing using sqlfiddle and your sample data set. http://sqlfiddle.com/#!3/8d7f6/29

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

Sidebar

Related Questions

Here is sample data <table class=sparql border=1> <tr> <th>abstract</th></tr> <tr> <td> Cologne is Germany&#39;s
Data: Here is sample table(TableA) data ID StartTime EndTime 1 2012-03-22 06:00:00.000 2012-03-22 06:30:00.000
I have a database table structure as follow: datatype data mytable now datatype has
Here is the sample code, which produces interesting output: > gg<-data.frame(x=c(a,b),y=as.integer(c(1000,100000))) > gg x
We have a simple Table per Type Entity Framework 4.0 model :- ALl classes
All the data is being passed as I have sufficiently tested over and over
I have the following table: if object_id(N'dbo.Node') is null create table dbo.Node ( ID
Here is a simple form I am using to send XML data in admin_xml.php
here the sample case.. i want to display banner randomly by percentage based on
Here's an sample code, where only the string object is released. NSString *nameOfFile =

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.