Subject Re: SQL syntax for 4 table query?
Author lysander4445
--- In firebird-support@yahoogroups.com, "petesouthwest"
<petesouthwest@h...> wrote:

Hello,
excuse me if I misunderstood your problem, in particular I did not
understand for what you need the additional grouping.
But will the following help to you?

select
tab1.key1,
tab1.col11,
sum(tab2.field),
sum(tab3.field),
sum(tab4.field),
sum(tab5.field)
from
tab1
join
tab2
on tab2.key1 = tab1.key1
join
tab3
on tab3.key1 = tab1.key1
join
tab4
on tab4.key1 = tab1.key1
join
tab5
on tab5.key1 = tab1.key1


(note, please, that this is by far not the best query, but it can help
to understand the query-logic)



>
> Hi
>
> Can anyone tell me what the syntax is to sum the data from 4
> different tables?
>
> I have tried:
> SELECT tab1.key1,
> tab1.col11,tot2.sum2,tot3.sum3,tot4.sum4,tot5.sum5
> FROM tab1
> JOIN (SELECT key1,SUM(field) AS sum2
> FROM tab2
> GROUP BY key1) AS tot2
> ON tot2.key1 = tab1.key1
> JOIN (SELECT key1,SUM(field) AS sum3
> FROM tab3
> GROUP BY key1) AS tot3
> ON tot3.key1 = tab1.key1
> JOIN (SELECT key1,SUM(field) AS sum4
> FROM tab4
> GROUP BY key1) AS tot4
> ON tot4.key1 = tab1.key1
> JOIN (SELECT key1,SUM(field) AS sum4
> FROM tab5
> GROUP BY key1) AS tot5
> ON tot5.key1 = tab1.key1
>
> But when i try and test this it appears to object about the
> first '(select' statement.
>
> Thanks for any help given
> Pete