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. Problem with this query is that this query is Selecting all records according to user_id from accounts table. I want to select records only created BETWEEN '2013-11-04' AND '2013-11-11'
  2. I want to select these records and need to Insert records also in the same query

Requirement: I want to select record from accounts table from last week group by user_id, Sum(Debit) and SUM(Credit) and Del ->(credit - debit) put the Sum and Insert records in same accounts table.

1 Answer 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
Sign up to request clarification or add additional context in comments.

1 Comment

One more condition in this query, select all records where del value is greater than 0, and if del is 0 then don't show that record

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.