Group By query

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

How to create a select query with two tables (one to many relationship) that
the query can give the number of record (group by count) from a field at the
one side of the table and the sum of a field from the many side of the
table. For example it has 10 records in the one side of the table and 50
related records in the many side of the table, when I group by count on the
id field of the one side of the table it comes back with 50 but it should be
10.
 
Could you take another stab at describing exactly what you need to do.
I think it would help me get it if you considered there to be only a
single record on the left side, with many children on the right side.
I can't tell if you mean you have 10 records with 50 children each, or
10 records with 50 children split among them.

I think this might be what you are looking for, though. I have
assumed

table t1 ( id_left AUTONUMBER )
table t2 (id_right AUTONUMBER, id_left NUMBER *FK* )

SELECT t1.id_left, Count(t2.id_right) AS ChildCount
FROM t1 INNER JOIN t2 ON t1.id_left = t2.id_left
GROUP BY t1.id_left;

In the case of T1 having the following data : 1, 2
and t2 having :
id_rt id_left
1 1
2 1
3 2
4 2
5 2
6 2

would return :

id_left ChildCount
1 2
2 4

Is this what you are looking for?

How to create a select query with two tables (one to many relationship) that
the query can give the number of record (group by count) from a field at the
one side of the table and the sum of a field from the many side of the
table. For example it has 10 records in the one side of the table and 50
related records in the many side of the table, when I group by count on the
id field of the one side of the table it comes back with 50 but it should be
10.

..--------------------------------------
| Andrew Backer
| backer_a @ h0tmai1 dot com
`--
 
Back
Top