Saturday, December 7, 2013

MySQL query for total sales per month and payment type

Table :
uid | invoice_num | pay_type | trans_date | total

Output :
Trans_date | Cash | Debit | Credit | Total
2013-10-01    1000      0          500     1500


Sql :

SELECT trans_date, 
    CASE WHEN pay_type = 0 THEN sum(total) ELSE 0 END AS Cash, 
    CASE WHEN pay_type = 1 THEN sum(total) ELSE 0 END AS Debit, 
    CASE WHEN pay_type = 2 THEN sum(total) ELSE 0 END AS Credit, 
    sum(total) AS Total
FROM tb_detail_transjual
GROUP BY trans_date

 

No comments:

Post a Comment