SELECT user_id, description, SUM( Credit ) AS cre, SUM( debit ) AS deb,
CASE WHEN credit > debit
THEN SUM( credit - debit )
END AS del, price, created
FROM accounts
WHERE created
BETWEEN '2013-11-04'
AND '2013-11-11'
AND description LIKE '%Amount Earned%'
OR description = 'S'
OR description = 'B'
GROUP BY user_id
要求:我想通过user_id,Sum(借方)和SUM(Credit)以及Del - >(credit - debit)从上周组中的账户表中选择记录,将Sum和Insert记录放在同一账户表中。
答案 0 :(得分:2)
insert into (other table )
SELECT user_id, description, SUM( Credit ) AS cre, SUM( debit ) AS deb,
CASE WHEN credit > debit
THEN SUM( credit - debit )
END AS del, price, created
FROM accounts
WHERE
( created BETWEEN '2013-11-04' AND '2013-11-11' )
AND
( description LIKE '%Amount Earned%'
OR description = 'S'
OR description = 'B' )
GROUP BY user_id, created