Subject | [firebird-support] Re: union all and group by |
---|---|
Author | Svein Erling Tysvaer |
Post date | 2009-02-14T20:11:37Z |
Though, thinking about it Dimitry, it is possible without WITH (and
hence, with older Firebird versions) through using subselects:
select distinct coalesce(a.cola1, b.COLB1) as Col1,
coalesce((select sum(ta.cola2) from tablea ta where ta.cola1 = a.cola1),
0) as total1,
coalesce((select sum(tb.colb2) from tableb tb where tb.colb1 = b.colb1),
0) as total2
from tablea a
full join tableb b on a.cola1=b.colb1
Set
Svein Erling Tysvaer wrote:
hence, with older Firebird versions) through using subselects:
select distinct coalesce(a.cola1, b.COLB1) as Col1,
coalesce((select sum(ta.cola2) from tablea ta where ta.cola1 = a.cola1),
0) as total1,
coalesce((select sum(tb.colb2) from tableb tb where tb.colb1 = b.colb1),
0) as total2
from tablea a
full join tableb b on a.cola1=b.colb1
Set
Svein Erling Tysvaer wrote:
> No, this gives the wrong result for any column that has more than one
> column in both tablea and tableb. Running your query on his test data, I
> got this result:
>
> FOR1 30 10
> FOR2 40 12
> FOR3 60 2
>
> whereas he wanted:
>
> FOR1 30 10
> FOR2 20 6
> FOR3 60 2
>
> Set
>
> Dimitry Sibiryakov wrote:
>>> with ta as
>> May be "with" is overhead and he just need
>>
>> select tablea.cola1 as col1, sum(tablea.cola2) as total1,
>> sum(tableb.colb2) as total2
>> from tablea full join tableb on tablea.cola1=tableb.colb1
>>
>> SY, SD.