Question

here's what im trying to achive:

i have 2 sql tables: transactions and payplans bellow is the structures of 2 tables:

transactions

uid | plan | date       | payid | status
------------------------------------
12  | 3    | 1388534400 | 334 | 1
699 | 4    | 1388214400 | 335 | 1

payplans:

plan | plan_price
-------------------
3    | 9.99
4    | 19.99

with this query:

SELECT SUM(plan_price) 
  FROM transations AS t
       INNER JOIN payplans AS p
                  ON t.plan = p.plan
 WHERE t.status = '1'

i was able to calculate total sum of all "plan_price" rows, but i would like to have the price sum for every month starting jan 2013 for example:

jan-13 | 9.99

feb-13 | 29.99 etc.

Was it helpful?

Solution

For MySQL

SELECT date_format(FROM_UNIXTIME(t.date), '%b-%y') as mnth,
       SUM(plan_price) 
  FROM transations AS t
       INNER JOIN payplans AS p
                  ON t.plan = p.plan
 WHERE t.status = '1'
 GROUP BY mnth;

SQLFiddle

  1. You converting unix_timestamp to date using FROM_UNIXTIME
  2. formatting it into 'MON-YY' format with DATE_FORMAT
  3. then grouping by month.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top