我创建了以下查询,但我真正想要的是,而不是SUM( credit - debit ) AS del
我需要SUM( credit - SUM of debit (Start Counting debit after transaction where description is 'Earned' ) AS del
。例如,从以下查询中,我得到了在描述为“已赚”的交易之前插入的借方值,并且我想在将交易“已赚”插入表中之后对借方列求和
SQL Fiddle 链接: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