I have a table in this format, with more N values and N vendors, approximately 100,000 records, and I need to do through a query or a plpgsql function a way to get all the launch numbers num
where the sum of valor_conta
is 0 (zero) or where the records of the debito
and credito
fields total valor_calculo
. In the example below would be the nums 4302454
, 4304304
, 4330098
, 4330095
. However, these records may not be displayed continuously, 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've tried to do several joins, including the form below, which takes the records that add up to what adds up to the balance amount, but does not work properly when the record is unique and total the balance amount. / p>
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
Would anyone help me with this problem?