Solved

Complicated sql query with 3 one to many tables.

Posted on 2004-08-05
5
831 Views
Last Modified: 2008-01-09
Hola.

I have 3 tables.

1.  Parent.
    PID
    PNAME

2.  Child.
     CID
     PID_fk
     CNAME

3.  ChildAge
     CAID
     CID_fk
     Age
     Height

What I want is a select query that will return the age of the children in one column and their heights at these ages in the next columns.  So something like:

Age    Martin   Steve   Johan
1        0.4       0.5       0.6
2        0.6       0.7       0.7
3        1.0       1.2       1.15

So I dont mind building a query for each parent and I need to be able to handle upto 9 children so I dont need an exponentially complicated query.

HELP!

Asim

0
Comment
Question by:asiminator
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
seazodiac earned 250 total points
ID: 11727000
let's say 9 is the upper limit (in my example, the name are arbitary)

select age, max(decode(upper(name), 'MARTIN', height, 0)) "Martin",
max(decode(upper(name), 'STEVE', height, 0)) "Steve",
max(decode(upper(name), 'JOHAN', height, 0)) "Johan",
max(decode(upper(name), 'SMITH', height, 0)) "Smith",
max(decode(upper(name), 'JACK', height, 0)) "Jack",
max(decode(upper(name), 'MIKE', height, 0)) "Mike",
max(decode(upper(name), 'RICHARD', height, 0)) "Richard",
max(decode(upper(name), 'TERESA', height, 0)) "Teresa",
max(decode(upper(name), 'BONNIE', height, 0)) "Bonnie"
from (select A.cid, A.name, B.age, B.height
from child A, childage B
where A.cid = B.cid_fk
and upper(A.name) in ('MARTIN', 'STEVE', 'JOHAN', 'SMITH', 'JACK', 'MIKE', 'RICHARD', 'TERESA', 'BONNIE'));

0
 
LVL 8

Expert Comment

by:sapnam
ID: 11727118
Are you going to use SQL or some tool like Report Builder finally to display this output.
0
 

Author Comment

by:asiminator
ID: 11727341
thanks for the rapid response.

I still have a problem.... when I run the query it says age is not a single-group group function.

Any ideas?

Asim
0
 
LVL 23

Expert Comment

by:seazodiac
ID: 11727358
yes, that's a syntax error, i fixed in the query below:

select age, max(decode(upper(name), 'MARTIN', height, 0)) "Martin",
max(decode(upper(name), 'STEVE', height, 0)) "Steve",
max(decode(upper(name), 'JOHAN', height, 0)) "Johan",
max(decode(upper(name), 'SMITH', height, 0)) "Smith",
max(decode(upper(name), 'JACK', height, 0)) "Jack",
max(decode(upper(name), 'MIKE', height, 0)) "Mike",
max(decode(upper(name), 'RICHARD', height, 0)) "Richard",
max(decode(upper(name), 'TERESA', height, 0)) "Teresa",
max(decode(upper(name), 'BONNIE', height, 0)) "Bonnie"
from (select A.cid, A.name, B.age, B.height
from child A, childage B
where A.cid = B.cid_fk
and upper(A.name) in ('MARTIN', 'STEVE', 'JOHAN', 'SMITH', 'JACK', 'MIKE', 'RICHARD', 'TERESA', 'BONNIE'))
group by age;
0
 

Author Comment

by:asiminator
ID: 11727445
cool.... thanks.

Youre a champ!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PL SQL Developer 7 53
Oracle collections 15 27
Input a SQl, output tables amd columns used in the SQL 19 55
Oracle create type table from existing table%rowtype ? 6 35
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question