Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql query: going crazy

Tags:

mysql

I'm having some trouble running a query on a table like this:

+-----+---------------------+-------+------+
| id  | paid_date           | amount| type |
+-----+---------------------+-------+------+
| 204 | 2010-10-22 05:12:54 |  1000 |    0 |
| 205 | 2010-10-22 05:13:12 |  1000 |    1 |
| 206 | 2010-10-21 05:13:44 |  1000 |    0 |
| 208 | 2010-10-22 05:57:33 |  1000 |    1 |
+-----+---------------------+-------+------+

The type column determines whether the money comes in or out, so I'd like to run a query that could gave me this result

+---------------------+-------+------+
| DATE(paid_date)     | in    | out  |
+---------------------+-------+------+
| 2010-10-21          |  1000 |    0 |
| 2010-10-22          |  1000 | 2000 |
+---------------------+-------+------+

I don't know what am I doing wrong, I know it's not so difficult, but can't make it happen :(

like image 614
ricardocasares Avatar asked Jan 02 '26 07:01

ricardocasares


1 Answers

   SELECT DATE(paid_date), 
   SUM(CASE type when 0 then amount else 0 end) in,
   SUM(CASE type when 1 then amount else 0 end) out
   FROM Table
   GROUP BY DATE(paid_date)
like image 104
Michael Pakhantsov Avatar answered Jan 04 '26 12:01

Michael Pakhantsov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!