If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Group By query
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. |
#2
|
|||
|
|||
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? On Tue, 24 Aug 2004 12:48:53 -0700, "Paul" wrote: 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 `-- |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Display Parameter from Form on Report | sara | Setting Up & Running Reports | 10 | July 19th, 2004 04:54 PM |
Hidden files in Ms-Query cause ODBC connect errors or Query is wac | needyourhelp | General Discussion | 4 | July 12th, 2004 09:38 PM |
Struggling with MS Query... | Alex | General Discussion | 5 | July 6th, 2004 11:46 AM |
SELECT function in Query | alexparks | Running & Setting Up Queries | 9 | July 5th, 2004 11:31 AM |
query field reference help | -dch | Running & Setting Up Queries | 4 | June 2nd, 2004 07:30 PM |