次のクエリを作成しましたが、私が正確に望むのは、SUM( credit - debit ) AS del
I needではなく、それですSUM( credit - SUM of debit (Start Counting debit after transaction where description is 'Earned' ) AS del
。たとえば、次のクエリから、説明が「獲得済み」であるトランザクションの前に挿入された借方値を取得しており、トランザクション「獲得済み」がテーブルに挿入された後に借方列を合計したい
SQL フィドルのリンク: http://sqlfiddle.com/#!2/12e46/4
SELECT user_id, 'Removed', 0, SUM( credit - debit ) AS del, 0, NOW( ), NOW( )
FROM accounts
WHERE created
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL DAYOFWEEK( NOW( ) ) +6
DAY )
AND DATE_SUB( DATE( NOW( ) ) , INTERVAL DAYOFWEEK( NOW( ) ) -1
DAY )
AND (
description LIKE '%Earned%'
OR description = 'S'
OR description = 'B'
)
GROUP BY user_id
HAVING del > 0