我创建了以下查询,但我真正想要的是而不是SUM( credit - debit ) AS del
我需要SUM( credit - SUM of debit (Start Counting debit after transaction where description is 'Earned' ) AS del
。
例如,从下面的查询我得到的借方值是在事务之前插入的,其中描述为'已获得',并且我希望在事务'已获得'被插入表格后的SUM借记列
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