I’ve got a simple table that has a column called Date and it stores ‘2015-01-15’ data as an example, it ranges in ‘####-##-##’ and there’s multiple of the same dates, but following it is a number called Amount with values 0, -## and ##.
How would I go through to making it sum up all the negative and positives separately per month?
Example data
[ Amount | Date ]
[ 0 | 2015-01-01]
[ -15 | 2015-01-09]
[ 566 | 2015-01-15]
[ 6 | 2015-01-25]
[ -2 | 2015-02-01]
[ -15 | 2015-02-09]
[ 10 | 2015-02-15]
[ 6 | 2015-02-25]
So the new data would be in
[ Positives | Negatives | Date ]
[ 572 | -15 | 2015-01]
[ 16 | -17 | 2015-02]
And so on
2
Answers
Use conditional aggregation:
Date functions vary significantly between databases. Although not standard, most support
year()
andday()
. However, you might need different functions for your database.You can do conditional aggregation;
least()
andgreatest()
come handy to shorten the query.Assuming that you are running MySQL, like the
phpmyadmin
tag suggest: