0

A table with a string field where date is being stored (don't suggest me to change the field type. It will cause hazard).

+--+----------+------+
|id|date      |amount|
+--+----------+------+
|1 |23-03-2014|5000  |
+--+----------+------+
|2 |25-03-2014|3000  |
+--+----------+------+
|3 |21-04-2014|3000  |
+--+----------+------+
|4 |25-04-2014|4000  |
+--+----------+------+

I want to write a model function which will return sum of amount grouped by month from the table. How should I do it? It will show output like

03 - 8000
04 - 7000
S. M. Shahinul Islam
  • 2,780
  • 4
  • 36
  • 68

3 Answers3

1
SELECT MONTH(date_field) as mon, SUM(amount) as total FROM table_name GROUP BY MONTH(date_field);

Update:

SELECT SUBSTR(date_field,6,2) as mon, SUM(amount) as total FROM table_name GROUP BY mon;
0

You can try following query

SELECT SUM(amount) FROM table GROUP BY MONTH(date)
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
0

Try MONTH()

SELECT MONTH(date),SUM(amount) FROM table GROUP BY MONTH(date)
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44