I need to get the total price per month.
I have
- inited_time (ex. 20160530105130)
- first_payment_price
- deleted
I tried grouping by inited_time
SELECT inited_time, DATE_FORMAT(inited_time, '%c') as month, SUM(first_payment_price) as price
FROM some_table
WHERE inited_time > 0 AND deleted = 0
GROUP BY inited_time
But it doesn’t sum them together.
ex of result:
[
{
"inited_time": 20160530105130,
"month": "5",
"price": 25000.00
},
{
"inited_time": 20160530105157,
"month": "5",
"price": 100000.00
},
{
"inited_time": 20160610000002,
"month": "6",
"price": 75000.00
},
{
"inited_time": 20160617000001,
"month": "6",
"price": 50000.00
},
{
"inited_time": 20160701000001,
"month": "7",
"price": 80000.00
},
{
"inited_time": 20160702000001,
"month": "7",
"price": 200000.00
}
]
3
Answers
Ideally the
inited_time
column would be a format date or timestamp. Assuming it is fixed width, we can aggregate by month using division and modulus on this column to isolate the month number:Assuming you might have multiple years in your data set and you would want to report each year month separately, we can try using
STR_TO_DATE
followed byDATE_FORMAT
:Simply, group by month and remove inited_time from the select clause
Edit. If you have different years included add another condition
DATE_FORMAT(20160530105130, '%Y')
, or if you want only one year even though you have different years filter the needed year on the where clause. For example for 2016 year the condition would beDATE_FORMAT(inited_time, '%Y') = 2016
group by
month