Ary Maulana
Ary Maulana

Reputation: 47

MySQL Sum with condition

i had table like this

----------------------------------------------------------------------
| TransactionDate | Price      | DownPayment | Status   |  DatePaid   |
 ---------------------------------------------------------------------
 2018-01-03       | 1000       | 1000        | PAID OFF |  2018-01-03
 2018-01-03       | 1000       | 500         | -500     |  2018-01-03
 2018-01-01       | 1000       | 0           | PAID OFF |  2018-01-03

based on the table, i had 3 row, the 1st is when someone made transaction and just paid off their transaction, the 2nd is someone paid half of the price, the last is when someone did the transaction before, but just paid it today.

i want to make sum of the money i got today based on the datepaid, on that table, the SUM should be 2500.

Upvotes: 0

Views: 1309

Answers (1)

In mysql we can make use of case statement something like this

select SUM(CASE WHEN Status = 'PAID OFF' THEN Price ELSE Price/2 END) as total where date(DatePaid) = CURDATE()

But the table design is not so good to perform these kind of aggregate functions. Especially the status column.

Upvotes: 2

Related Questions