Skip to main content
pequena correção
Fonte Link

Para versões de MySQL acima de 5>= 8.70,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 2, 3;

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 2, 3;

Para versões de MySQL >= 8.0,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 2, 3;
pequena correção
Fonte Link

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 12, 2;3;

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 1, 2;

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 2, 3;
pequena correção
Fonte Link

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT 1C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT QC.data, QC.fornecedor, QHCRED.creditovalor_pago, QHDEB.debitovalor_pago, COALESCE(QHCRED.creditovalor_pago, 0) + COALESCE(QHDEB.debitovalor_pago, 0) + Q.saldo
    FROM Qtb_compras C
    INNER JOIN tb_comprasQ ON C.id ON= Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id += 1HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 1, 2;
WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT 1, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT Q.data, Q.fornecedor, Q.credito, Q.debito, COALESCE(Q.credito, 0) + COALESCE(Q.debito, 0) + Q.saldo
    FROM Q
    INNER JOIN tb_compras C ON Q.rowid = C.id + 1
)
SELECT *
FROM Q
ORDER BY 1, 2;

Para versões de MySQL acima de 5.7,

WITH RECURSIVE Q (rowid, data, fornecedor, credito, debito, saldo) AS
(
    SELECT C.id, C.data, C.fornecedor, HCRED.valor_pago as credito, HDEB.valor_pago as debito, COALESCE(HDEB.valor_pago, 0) + COALESCE(HCRED.valor_pago, 0) as saldo
    FROM      tb_compras C
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0

    UNION

    SELECT C.data, C.fornecedor, HCRED.valor_pago, HDEB.valor_pago, COALESCE(HCRED.valor_pago, 0) + COALESCE(HDEB.valor_pago, 0) + Q.saldo
    FROM tb_compras C
    JOIN Q ON C.id = Q.rowid + 1
    LEFT JOIN tb_compras_historico_pagamento HCRED
              ON C.id = HCRED.id_compra AND HCRED.valor_pago > 0
    LEFT JOIN tb_compras_historico_pagamento HDEB
              ON C.id = HDEB.id_compra AND HDEB.valor_pago < 0
)
SELECT *
FROM Q
ORDER BY 1, 2;
adicionei conteúdo
Fonte Link
Carregando
Fonte Link
Carregando