Skip to main content
added 3 characters in body
Source Link

This is one way:

SELECT
    x.*
FROM 
(
    SELECT
        invoices.*,
        SUM(payments.amount) as paid
    FROM
        invoices
    LEFT JOIN
        payments ON payments.invoice = invoices.id
    GROUP BY
        invoices.id
) AS x
WHERE x.paid < x.total_amount 
AND   x.paid > 0;

Due to how logical query processing works, columns (like aggregates) that are expressions in the select list can't be used in portions of the query that are processed before them.

This is one way:

SELECT
    x.*
FROM 
(
    SELECT
        invoices.*,
        SUM(payments.amount) as paid
    FROM
        invoices
    LEFT JOIN
        payments ON payments.invoice = invoices.id
    GROUP BY
        invoices.id
) AS x
WHERE x.paid < x.total_amount 
AND   x.paid > 0;

Due to how logical query processing works, columns (like aggregates) that are expressions in the select list can't be used portions of the query that are processed before them.

This is one way:

SELECT
    x.*
FROM 
(
    SELECT
        invoices.*,
        SUM(payments.amount) as paid
    FROM
        invoices
    LEFT JOIN
        payments ON payments.invoice = invoices.id
    GROUP BY
        invoices.id
) AS x
WHERE x.paid < x.total_amount 
AND   x.paid > 0;

Due to how logical query processing works, columns (like aggregates) that are expressions in the select list can't be used in portions of the query that are processed before them.

Source Link

This is one way:

SELECT
    x.*
FROM 
(
    SELECT
        invoices.*,
        SUM(payments.amount) as paid
    FROM
        invoices
    LEFT JOIN
        payments ON payments.invoice = invoices.id
    GROUP BY
        invoices.id
) AS x
WHERE x.paid < x.total_amount 
AND   x.paid > 0;

Due to how logical query processing works, columns (like aggregates) that are expressions in the select list can't be used portions of the query that are processed before them.