dbTalk Databases Forums  

Urgent help please

comp.databases.theory comp.databases.theory


Discuss Urgent help please in the comp.databases.theory forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Alan
 
Posts: n/a

Default Urgent help please - 08-19-2003 , 01:54 AM






I have a table contains parent and children information,
this is not the actual table that I am working with but has similar
structure:

ID Name Parent NoOfChildren Age
1 John NULL 3 50
2 Mary 1 0 20
3 Peter 1 0 19
4 David 1 0 30
5 Alan NULL 1 60
6 Candy 5 0 25
7 Bobby NULL 0 35
8 Vincent NULL 0 18


John, age of 50, is the parent of 3 children, Mary, Peter and David.
Alan, age of 60 has 1 child, Candy.
Boby has no children.
Vincent has no children.


I am using MySQL so does not support subquery.
How do I get the names of the parent when satifiy one of the following
conditions:
1) is over 20 years old and have no children
2) is over 20 years old and if his children who are ALL over 20 years old.

Result of the above table:
ID Name Age
5 Alan 60
7 Bobby 35

John is not included because he has child Peter who is 19.
Vincent is a parent who is only 18 so also excluded.



Reply With Quote
  #2  
Old   
Daniel Guntermann
 
Posts: n/a

Default Re: Urgent help please - 08-20-2003 , 07:37 AM







"Harald Fuchs" <nospam (AT) sap (DOT) com> wrote

Quote:
In article <3f41c994$1 (AT) news (DOT) comindico.com.au>,
"Alan" <alanpltse (AT) yahoo (DOT) com.au> writes:

I have a table contains parent and children information,
this is not the actual table that I am working with but has similar
structure:

ID Name Parent NoOfChildren Age
1 John NULL 3 50
2 Mary 1 0 20
3 Peter 1 0 19
4 David 1 0 30
5 Alan NULL 1 60
6 Candy 5 0 25
7 Bobby NULL 0 35
8 Vincent NULL 0 18

Drop NoOfChildren since it violates normalization.

Beg to differ. It might be logically redundant, but, afaik, it does not
violate the definition of a functional dependency, nor does it violate
normal forms. A relational expression value and a tuple attribute value
both will be functionally dependent on the key and nothing but the key.

A table constraint would probably be beneficial in ensuring consistency
between the derived value resulting from the relational expression and the
tuple value, but this is an integrity issue, not a normalization issue.

Regards,

Daniel Guntermann




Reply With Quote
  #3  
Old   
Alan
 
Posts: n/a

Default Re: Urgent help please - 08-21-2003 , 08:56 PM



Candy is not included before she is a child of Alan.
I only want to include the top level parent.

Quote:
I have a table contains parent and children information,
this is not the actual table that I am working with but has similar
structure:

ID Name Parent NoOfChildren Age
1 John NULL 3 50
2 Mary 1 0 20
3 Peter 1 0 19
4 David 1 0 30
5 Alan NULL 1 60
6 Candy 5 0 25
7 Bobby NULL 0 35
8 Vincent NULL 0 18
Why is Candy not included? She's 25 and has no children. Same with
David, 30 and no children. Anyhow, here is one attempt:



Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Urgent help please - 08-22-2003 , 01:09 AM



"Alan" <alanpltse (AT) yahoo (DOT) com.au> wrote

Quote:
Candy is not included before she is a child of Alan.
I only want to include the top level parent.

select
x1.name, x1.age
from
x x1, x c
where
x1.age > 20 and x1.parent is null
group by
x1.name, x1.age
having
count(case when c.parent = x1.id and c.age < 20 then 1 else null
end) = 0


if "CASE WHEN ..." is not supported by mysql, you could perhaps use
"IF ..."? I've seen "IF ..." in Q/A for mysql, so I guess "CASE ..."
is not supported?


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.