Sie sind auf Seite 1von 2

SQL unlimited depth recursive Chart of Accounts, postgres & sqlite3 version Page 1 sur 2

SQL unlimited depth recursive Chart of


Accounts, postgres & sqlite3 version
Wm wm+gnc at tarrcity.demon.co.uk
Thu Dec 25 00:47:24 EST 2014

Previous message: SQL unlimited depth recursive Chart of Accounts, postgres version
Next message: Writing custom reports for Customers
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

Fri, 12 Dec 2014 21:34:09 <Kk8X0ITR91iUFwIR at tarrcity.demon.co.uk> Wm


<wm+gnc at tarrcity.demon.co.uk>

[ff to self, snips below]

>Here is a (for practical purposes, it works with 10 levels) unlimited


>depth recursive Chart of Accounts in postgres. It shouldn't need much
>done to it to work in sqlite (ask if you're interested, I have sqlite
>handy) but will require non-trivial fiddling to work in mysql if my
>reading is correct as mysql doesn't have WITH RECURSIVE at the moment.

> lpad(''::text, tree_1.depth * 4) || a.name::text AS


>name_tabs,

SQLite3 doesn't have lpad but both SQLite3 and postgres have substr and
they do the same thing so identical SQL can be used (I've stripped out
some casting to (hopefully) make the code more readable).

the revised query is


===
WITH RECURSIVE tree (
guid
,parent_guid
,NAME
,name_tree
,name_tabs
,account_type
,depth
)
AS (
SELECT guid
,parent_guid
,NAME
,'' || NAME AS name_tree
,'' AS name_tabs
,account_type
,0 AS depth
FROM accounts
WHERE parent_guid IS NULL
AND NAME <> 'Template Root'

UNION ALL

SELECT a.guid
,a.parent_guid
,a.NAME
,tree.name_tree || ':' || a.NAME AS name_tree
,substr('.................................',1,depth*2)

http://lists.gnucash.org/pipermail/gnucash-user/2014-December/057344.html 25/11/2016
SQL unlimited depth recursive Chart of Accounts, postgres & sqlite3 version Page 2 sur 2

|| a.NAME AS name_tabs
,a.account_type
,depth + 1 AS depth
FROM tree
JOIN accounts a
ON tree.guid = a.parent_guid
)
SELECT *
FROM tree
ORDER BY name_tree ;
===

Caveats:

1. beware of your favourite db tool not knowing SQLite3 as well as it


should :) It took me a few iterations to realise my sql was good but
that my chosen db tool didn't understand WITH RECURSIVE properly so I
went back to the command line [2]

[2] don't let that put you off trying things out, this doesn't change
anything in your accounts

2. there is an arbitrary bit in the substr in that if you have a very


deeply nested CoA you need more dots / tabs / spaces but that seems a
reasonable overhead to me in having 2 out of 3 of the gnc sql backends
working with identical code [1]

[1] sensibly I know I should be using SQLite3 as my base as that is the


one most people are likely to use in future and I could probably make it
something other than a string but for my purposes it works for now, let
me know if it bugs you.

===

Presuming no-one tried this out before (I think postgres is the minority
backend) would you like to see a chart of accounts or balance sheet ?

You should be paying attention because this is a lot simpler than


messing with the current reporting methods and likely the way forward.

--
Wm...

Previous message: SQL unlimited depth recursive Chart of Accounts, postgres version
Next message: Writing custom reports for Customers
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

More information about the gnucash-user mailing list

http://lists.gnucash.org/pipermail/gnucash-user/2014-December/057344.html 25/11/2016

Das könnte Ihnen auch gefallen