Subject Re: [firebird-support] help with a select, please!
Author Fidel Viegas
On Fri, Apr 3, 2009 at 9:27 PM, Sergio H. Gonzalez
<shg_sistemas@...> wrote:
>> Assuming the first of your tables to be named CHARTS (OK, not the best
>> name) containing a field to sum named MyField and the latter, linking
>> table to be called ACCOUNTS, the following SQL should get you the sum
>> for the group you desire:
>>
>> WITH RECURSIVE GROUPCONTAINS(SUBCODE, CODE)
>> AS (SELECT A.CODE, A.SUMMARIZES_IN
>> FROM ACCOUNTS A
>> WHERE A.SUMMARIZES_IN = :CODE
>> UNION ALL
>> SELECT A.CODE, A.SUMMARIZES_IN
>> FROM GROUPCONTAINS G
>> JOIN ACCOUNTS A ON G.SUBCODE = A.SUMMARIZES_IN)
>>
>> SELECT SUM(C.MyField) FROM GROUPCONTAINS G
>> JOIN CHARTS C ON G.SUBCODE = C.CODE
>
> Thanks Svein!!!
>
> It does exactly what I need... the only problem is that I understand how it
> works, in the same way I undestand the Big Bang!! :) mmhh... I'd love to
> read
> some article about recursive sql with firebird... any chance you know where
> I
> can find it? I've tried with the documentation of FB, but couldn't find any
> tutor...
>

Vlad has written a nice on that. Here it is
http://www.ibphoenix.fr/IMG/pdf/Firebird.2.1.2007.en.pdf

You can also do a search on google on recursive CTE, and you should
find quite a few about SQL server. The syntax is the same, so you can
follow the tutorials on that as well.

Fidel.