Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SELECT to combine master and 2 child tables
Message
From
11/05/2004 01:12:38
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00902663
Message ID:
00902697
Views:
20
I added a "GROUP BY h.pid_owner, h.ord_cod" to it. The totals were incorrect. If there were 2 dtl and 2 inv records, then it summed both dtl records for each inv record (4 dtl records summed). Make 3 simple test tables (Master and 2 child tables) and try it.

-----
Lee Perkins

>Actually, I did forget one important piece to that query. You need to have a GROUP BY pid_owner, ord_code. It doesn't matter if there are different numbers of records for each of the children. The case where you wouldn't want to use an INNER JOIN is if you want to see the parent if one or the other child tables doesn't have any match for that parent.
>
>>Will doing INNER JOINs work when the two child tables will have differnt number or records? For 1 qn_head record, there may be 5 qn_dtl records, and 3 qn_inv records. When I tried doing joins in SQL Server, it got confused and summed the wrong number of rows. I'll try it and see what I get. I'll let you know. :)
>>
>>-----
>>Lee Perkins
>>
>>>You're correct in that VFP doesn't support sub-queries of this type. (I think VFP 9 will, but that won't be out for a while.) Here's how you need to do this:
>>>
>>>SELECT h.pid_owner, h.ord_cod, ;
>>>       SUM(d.itm_prcus * d.qty_ord) as OrderTotal, ;
>>>       SUM(i.itm_prcus * i.qty_sent) as InvoiceTotal ;
>>>   FROM C:\QN\datatmp\qn_head h ;
>>>      INNER JOIN C:\qn\datatemp\qn_dtl d ;
>>>         ON d.pid_owner = h.pid_owner AND d.ord_cod = h.ord_code ;
>>>      INNER JOIN c:\qn\datatemp\qn_inv i ;
>>>         ON i.pid_owner = h.pid_owner AND i.ord_code = h.ord_cod ;
>>>   ORDER BY h.pid_owner, h.ord_cod
>>>
-----
Lee Perkins
TigerBase Technologies

"Lee is one that would plug his brain into the internet, if he could, and STILL scream for more" - Very good friend of Lee's
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform