user2645436
user2645436

Reputation: 64

Month wise sum of column mysql

Hello i am using php and mysql.I have one table with one column as recdatetime as datetime datatype .

If i search record between two months i want sum of quantity column of same table.

If i have following record.

quantity recdatetime
**44** `2014-01-01 16:53:06`
**14** ` 2014-01-21 16:53:06`
**10** `2013-12-21 16:53:06 `
**17** `2013-12-22 16:53:06 `
**29** `2013-11-20 16:53:06`

If i search between November 2013 and January 2014 i want output in following manner.

November December January    
29        27       58

I want Mysql query for above output.

Upvotes: 0

Views: 2379

Answers (3)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

You can try like this:

select sum(quantity) as sumQty,recdatetime FROM table_name group by Month(recdatetime

Upvotes: -1

Seb
Seb

Reputation: 1551

You can use month(recdatetime), sum(quantity) and group by month(recdatetime).

Select year(recdatetime), month(recdatetime), sum(quantity) as total from x
group by year(recdatetime),month(recdatetime);

You will need the year or it will also sum up months for different years. You normaly do not want this. And add a where condition to only get the months you want. Hope you can do that on your own.

Switching rows and columns could be done in the output.

Upvotes: 0

Glavić
Glavić

Reputation: 43552

Start with this:

SELECT SUM(quantity) AS s, DATE_FORMAT(recdatetime, '%M') AS m
FROM table_name
GROUP BY DATE_FORMAT(recdatetime, '%Y-%m')

Upvotes: 3

Related Questions