1
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
  1. 此查询的问题在于此查询是根据帐户表中的 user_id 选择所有记录。我想选择仅在“2013-11-04”和“2013-11-11”之间创建的记录
  2. 我想选择这些记录并且需要在同一个查询中也插入记录

要求:我想通过 user_id、Sum(Debit) 和 SUM(Credit) 从上周组的帐户表中选择记录,并且 Del ->(credit - debit) 将 Sum 和 Insert 记录放在同一个帐户表中。

4

1 回答 1

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
于 2013-11-11T12:41:45.957 回答