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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3050
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 2, 20222022-06-02T00:25:54+00:00 2022-06-02T00:25:54+00:00

postgresql – SQL query where has children with conditions

  • 0

[ad_1]

First we need to join the tables — like this

select p.id as p_id, p.name as parent_name,
       k.* -- we won't need this in later versions
from parent p
join kidatt k on p.id = k.parent_id

now we have two attributes we care about — let make a query that shows those

select p.id as p_id, p.name as parent_name,
       case when k.attribute="intelligence" and k.attribute_value = 5 then 1 else 0 end as has_a1,
       case when k.attribute="health" and k.attribute_value = 4 then 1 else 0 end as has_a2
from parent p
join kidatt k on p.id = k.parent_id

we now have a query with a 1 in a row for those that have each of these

now we group by the parent.

select p.id as p_id, p.name as parent_name,
       SUM(case when k.attribute="intelligence" and k.attribute_value = 5 then 1 else 0 end) as has_a1,
       SUM(case when k.attribute="health" and k.attribute_value = 4 then 1 else 0 end) as has_a2
from parent p
join kidatt k on p.id = k.parent_id
group by p.id, p.name

now we have a query where a1 and a2 are greater than 0 if one or more child has it.

Now just select the results

select * 
from (
  select p.id as p_id, p.name as parent_name,
       SUM(case when k.attribute="intelligence" and k.attribute_value = 5 then 1 else 0 end) as has_a1,
       SUM(case when k.attribute="health" and k.attribute_value = 4 then 1 else 0 end) as has_a2
  from parent p
  join kidatt k on p.id = k.parent_id
  group by p.id, p.name
)
where has_a1 > 0 and has_a2 > 0

note — I did not write this query to be the best way to solve this problem — instead I wrote it in a way to show you how to “think” in SQL and solve the problem with a series of steps.

I’d have to test to be sure, but I expect this would be the fastest way to do this query (depends on data and indexes etc.)

select distinct p.id as p_id, p.name as parent_name,          
from parent p
join kidatt k on p.id = k.parent_id
where k.attribute="intelligence" and k.attribute_value = 5

intersect

select distinct p.id as p_id, p.name as parent_name,          
from parent p
join kidatt k on p.id = k.parent_id
where k.attribute="health" and k.attribute_value = 4 

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 2 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 4 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 3 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.