Pieter888
Pieter888

Reputation: 4992

Get paid cash amount grouped by day

I have a table called orders this table contains all the information we need.

I'll just list the columns we need for the query:

The idea is pretty simple. we need to get the total amount of cash payed grouped by day. But we found this a pretty hard task.

PS: I'm playing around with this problem in PHP using MySQL, so it's alright to use multiple queries and/or use some PHP script.

Upvotes: 0

Views: 128

Answers (4)

Karthik
Karthik

Reputation: 1091

SELECT *, IF(`pay_method`=1,SUM(`cash_paid`),0) AS Cash_Paid FROM orders o
GROUP BY DATE(`date`)

Can you please try this. this will give you total cash paid... for partailly paid cash you can create another query and add them both....

Upvotes: 1

Konerak
Konerak

Reputation: 39763

Use MySQL IF-function:

If the pay_method is 1, take the purchase_price field. Else take the cash_paid field.

SELECT DATE(`date`), SUM(IF(pay_method=1,purchase_price,cash_paid)) 
FROM yourtable
/*WHERE pay_method IN (1,3) -- if you only want those */
GROUP BY DATE(`date`)

Upvotes: 3

Jake N
Jake N

Reputation: 10583

You need to use GROUP BY DAY(date)

Upvotes: 3

Adrian Serafin
Adrian Serafin

Reputation: 7715

In one query you could try like this:

select DATE(date), sum(case when pay_method = 1 then purchase_price else cash_paid end) from orders group by DATE(date)

btw. not sure if in mysql you can go with "case" inside sum function.

Upvotes: 2

Related Questions