Good afternoon, I have a tabla
that has clave
, clave_padre
, anio
and nivel
, and on the other hand I have a tabla2
that has the clave
, cargo
, abono
, mes
and anio
.
What I try is to join both tables delimited by anio
, mes
and nivel
, I tried to use group by rollup
without success because the group of accounts of tabla2
are only of the last level while the full catalog is in tabla
.
An example of what I expect is the following:
Table
Clave Clave_Padre Nivel Anio
----------------------------------------
123 -1 1 2015
123.1 123 2 2015
123.1.1 123.1 3 2015
123.1.2 123.1 3 2015
123.1.3 123.1 3 2015
Table2
Clave Cargo Abono Mes
---------------------------------
123.1.1 $50 $50 1
123.1.1 $0 $50 2
123.1.1 $0 $50 3
123.1.1 $0 $50 7
123.1.1 $0 $50 12
123.1.2 $10 $0 2
123.1.2 $10 $0 5
And what I want is to group them in the following way:
Result: 2015
Clave Nivel Mes SUM(Cargo) SUM(Abono)
----------------------------------------------------
123 2 1 $50 $50
123 2 2 $10 $50
123 2 3 $0 $50
123 2 5 $10 $0
123 2 7 $0 $50
123 2 12 $0 $50
123.1 2 1 $50 $50
123.1 2 2 $10 $50
123.1 2 3 $0 $50
123.1 2 5 $10 $0
123.1 2 7 $0 $50
123.1 2 12 $0 $50
123.1.1 3 1 $50 $50
123.1.1 3 2 $0 $50
123.1.1 3 3 $0 $50
123.1.1 3 7 $0 $50
123.1.1 3 12 $0 $50
123.1.2 3 2 $10 $0
123.1.3 3 5 $10 $0
I have tried to rollup without success the join and I have not obtained what I need, any suggestions?
Edited by @Patricio Moracho at 21/6
So that the question is better understood, I add the following explanation: In the accounting the chart of accounts is armed with a tree-like structure, according to the question it would be something like this:
Cuenta 123
Cuenta 123.1
Cuenta 123.1.1
Cuenta 123.1.2
Cuenta 123.1.3
Normally when you enter an amount (debit / credit) in a seat it is charged to the lower level account, in example 123.1.1 to 123.1.3, but that amount is added up, that is, 50 $ to 123.1.1 are also added to 123.1 and 123. The idea is to see the accounting with different levels of detail, a real example could be something like this:
Activo
Caja Bancos
Bancos Locales
Santander
ICBC
Bancos del exterior
UBS
Morgan
If we impute a $ 50 income or expenditure to the ICBC, what you would expect to see is the following:
Activo 50$
Caja Bancos 50$
Bancos Locales 50$
Santander
ICBC 50$
Bancos del exterior
UBS
Morgan
and if we then impute $ 100 to Morgan, we would end up expecting this:
Activo 150$
Caja Bancos 150$
Bancos Locales 50$
Santander
ICBC 50$
Bancos del exterior 100$
UBS
Morgan 100$
One way to solve it, I tell it by how I usually handle it, is to summarize upwards by means of an update / insert trigger in a table of account accumulators per period. What the question actually asks is to do it differently and that the accumulated up is resolved at the time of listing the data.