2
I have a table in this format, with more N values and N suppliers, approximately 100 thousand records, and I need to do through a query or a plpgsql function a way to get all the release numbers num
where the sum of valor_conta
total 0 (zero) or where the field records debito
and credito
totalize the valor_calculo
. In the example below would be the nums 4302454
, 4304304
, 4330098
, 4330095
. However, these records may not be continuously displayed, and may be in single value. There is no criterion in how they are registered or displayed.
num(pk) fornecedor debito credito valor_conta saldo valor_calculo 4302458 FORNECEDOR1 4.35 4.35 23.47 869.00 4302456 FORNECEDOR1 19.12 19.12 23.47 869.00 4302454 FORNECEDOR1 435.00 -435.00 23.47 869.00 4304304 FORNECEDOR1 460.00 460.00 23.47 869.00 4330098 FORNECEDOR1 409.00 409.00 23.47 869.00 4330095 FORNECEDOR1 434.00 -434.00 23.47 869.00
I have already tried to do several joins, including the form below, which takes the records that add up what adds up the amount of the balance, but does not work properly, when the record is unique and total the amount of the balance.
SELECT 'join'::character(4) as link, a.num, a.debito, a.credito
FROM conta_contabil.staging_livro_razao a
INNER JOIN (
SELECT 'join'::character(4) as link, a.num, a.debito, a.credito
FROM conta_contabil.staging_livro_razao a
WHERE a.fornecedor = 'FORNECEDOR1'
)b ON join' = b.link AND a.fornecedor = 'FORNECEDOR1'
WHERE a.debito + b.debito = 23.47
Someone could help me with this problem?
the table structure is exactly the same as it is there and the field in a (pk) is the same primary key ? and it is not repeated, correct ?
– Rovann Linhalis
you have to group by the supplier ?
– Rovann Linhalis
suggestion: use credit / debit in only one column, only varying the signal values
– Rovann Linhalis
and what is the order of the records ? date ? sequence ?
– Rovann Linhalis
@Rovannlinhalis then,
num
is the primary key, does not repeat. Yes I need to group byfornecedor
, because each supplier has different registration and values (this will deal with plpgsql). So, the debit and credit are in the columnvalor_conta
, the debit is positive and the credit is negative. There is no ordering. There is no rule to reset the balance (for example I have already ordered byvalor_conta
and I was calculating with the next value. There is a case where I find 0 (zero), and there are cases that do not.– thiagofred
Trying to simplify your need, you want to know which suppliers are with the balance 0, that’s it ?
– Rovann Linhalis
I need the records of every supplier that the balance totals zero. Because in this example the balance would be 23.47, I need to set the records that gave zero balance with True. For when I make a new query bring only the 2 records totaling 23.47.
– thiagofred
there is the possibility of changing the table structure ? and what is the meaning of the columns
valor_conta
,saldo
, andvalor_calculo
because with the values that are there does not make the slightest sense– Rovann Linhalis
take a look at this SQL if it helps you: http://sqlfiddle.com/#! 15/62113/1
– Rovann Linhalis
@Rovannlinhalis
valor_conta
is the junction of the columnsdebito
andcredito
. However, the debit is positive and the credit is negative. Columnsaldo
is equal toSUM(valor_conta)
, already grouped by supplier. Evalor_calculo
is theSUM(debito) - saldo
, already grouped by supplier.saldo
andvalor_calculo
serve only to base.– thiagofred
but if the values are equal on all lines, there’s something wrong with that logic. See the SQL I put in the fiddle ? there shows the balance to each movement, which would be the most logical
– Rovann Linhalis
in this query: http://sqlfiddle.com/#! 15/ed913/1 what you want is to have only the last record, where the current balance has reached zero ?
– Rovann Linhalis
@Rovannlinhalis the values is because as these data came from an excel file, in the ETL tool, I have already done this calculation. It has no bearing on the outcome I want, and I showed them just to demonstrate. I saw your SQL, but it still didn’t help me... let’s go to chat, let me try to explain it properly.
– thiagofred
Let’s go continue this discussion in chat.
– thiagofred