How to retrieve total amount of one column that is dependent by another column from mysql -


i retrieve day(dateandtime),sum(amount) group day(dateandtime) when type "withdraw",the amount subtracted instead of adding. how achieve that?

my query now:

  select day(dayandtime),sum(amount) table group day(dayandtime) 

table

id  |   username    |    type       |    amount     |   dateandtime     |               |               |               |  1  |    user1      |    withdraw   |       10      | 2016-01-27 03:35:07   2  |    user2      |    deposit    |       20      | 2016-01-27 03:35:07   3  |    user1      |    deposit    |       40      | 2016-01-27 03:35:07   4  |    user2      |    withdraw   |       20      | 2016-01-27 03:35:07      

you should work case statement this:

select day(dateandtime),sum(case when type='deposit' amount else -amount end)  table1 group day(dateandtime) 

you can test here


don't forget made typo in query

dayandtime 

should be

dateandtime 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -