0

#1111 - Invalid use of group function次のクエリは、両方の SUM() 呼び出しが IF ループ内に埋め込まれているために失敗し、不平を言って います。今では、別のSQLクエリに移行しながら同じ目標を達成しようとしていますが、苦労しています.誰かが私を正しい方向に向けることができますか?

データベースは、クライアントが請求書を一度に支払わず、代わりに分割払いを行った場合invoicesにいくつかのテーブルで構成されます。invoices_payments請求書.issued_dateが現在の週に等しい場合にのみ、現在の週に等しくない各invoices_payments.payment_dateを差し引いて、すべてのinvoices.invoice_totalを合計しようとしています。 .payment_date は現在の週と同じです。

SELECT
    SUM(

        IF (
            YEARWEEK(i.issue_date, 1) = YEARWEEK(NOW(), 1),
            i.invoice_total,
            0
        )
    ) AS week_invoiced,
    ##SUM Functions embedded inside the next IF are causing error: #1111 - Invalid use of group function
    SUM(

        IF (
            ## If payment is issued on current week
            i.issued_date IS NOT NULL
            AND YEARWEEK(i.issued_date, '%Y%m') = YEARWEEK(NOW(), '%Y%m'),
            ## Sum each invoice.invoice_total and substract each invoices_payments not issued on current week
            SUM(i.invoice_total) - SUM(

                IF (
                    ip.id IS NOT NULL
                    AND YEARWEEK(ip.payment_date, 1) != YEARWEEK(NOW(), 1),
                    ip.payment_amount,
                    0
                )
            ),

        IF (
            ## If payment is not issued on current week
            i.issued_date IS NULL
            OR (
                i.issued_date IS NOT NULL
                AND YEARWEEK(i.issued_date, '%Y%m') != YEARWEEK(NOW(), '%Y%m')
            ),
            ## Sum each invoices_payments issued on current week
            SUM(

                IF (
                    ip.id IS NOT NULL
                    AND YEARWEEK(ip.payment_date, 1) = YEARWEEK(NOW(), 1),
                    ip.payment_amount,
                    0
                )
            ),
            0
        )
        )
    ) AS week_paid,
    SUM(

        IF (
            DATE_FORMAT(i.issue_date, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m'),
            i.invoice_total,
            0
        )
    ) AS month_invoiced
FROM
    `invoices` AS i
INNER JOIN `currency` AS cur ON i.currency_id = cur.id
LEFT JOIN `invoices_payments` AS ip ON ip.invoice_id = i.id
WHERE
    i.coach_id = 279
AND (
    DATE_FORMAT(i.issue_date, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')
    OR DATE_FORMAT(i.issued_date, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')
    OR (
        ip.id IS NOT NULL
        AND DATE_FORMAT(ip.payment_date, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')
    )
)
GROUP BY
    i.id
4

1 に答える 1

0

問題のある外側は必要ありませんSUM。つまり、内側の SUM で十分です。

SUM( SUM()-SUM() )

-->

SUM()-SUM()
于 2015-04-22T19:39:47.437 に答える