Skip to content

Instantly share code, notes, and snippets.

@daum
Created April 23, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daum/76148cb00768de1c620b to your computer and use it in GitHub Desktop.
Save daum/76148cb00768de1c620b to your computer and use it in GitHub Desktop.
table:
user,balance,deposits,date
1,100,200,4/22
1,50,0,4/23
1,100,0,4/24
2,0,0,4/22
2,50,0,4/23
2,100,0,4/24
==============================================
SELECT user,
SUM(deposits) as total_deposits,
end_balance - start_balance - total_deposits as total_profit
FROM table t
LIMIT 1
===============================================
End goal -
Given a date range, return a users total deposits, and the top 1 user who make the biggest profit.
In this case, assuming date range 4/22-4/24, we'd want the following result:
2,0,100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment