I want to write a group-by sql just like credit card bills,its look like tell u the total amount in every billing month,but i have no idea how to do this. Its group by month like between 2017/11/10 and 2017/12/09,2017/10/10 and 2017/11/09 and more. It only provide a paramter about bill date,look like between 1 and 27, it constitute the day in date.
select
b.name,
SUM(a.total_security_amount) AS totalSecurityAmounts,
SUM(a.total_accumulation_amount) AS totalAccumulationAmounts,
SUM(a.total_salary_amount) AS totalSalaryAmounts,
SUM(a.total_other_amount) AS totalOtherAmounts,
SUM(a.service_amount) AS serviceAmounts,
SUM(a.tax_amount) AS taxAmounts,
SUM(a.total_amount) AS totalAmounts,
SUM(a.confirm_fund_amount) AS confirmFundAmounts,
SUM(a.total_insum_amount) AS totalInsumAmounts
from ims_invoice a
left join ims_customer_center b on a.cust_center_id = b.id
group by b.id
table A has the column create_date
Its need more a interval date paramter as group by?
the Year 2017 interval date like,the assumption bill day is 20,the follow bills month like
Month 12: 2017.11.21-2017.12.20
Month 11: 2017.10.21-2017.11.20
Month 10: 2017.09.21-2017.10.20
Month 09: 2017.08.21-2017.09.20
The select date show in the page like this.enter image description here
Now i have a idea,but probably look like so bad
select
date_format(a.create_date,'%Y-%m-%d'),
if(date_format(a.create_date,'%d')>20,date_format(DATE_ADD(a.create_date,INTERVAL 1 MONTH),'%m'),date_format(a.create_date,'%m')) AS month_group,
b.name,
SUM(a.total_security_amount) AS totalSecurityAmounts,
SUM(a.total_accumulation_amount) AS totalAccumulationAmounts,
SUM(a.total_salary_amount) AS totalSalaryAmounts,
SUM(a.total_other_amount) AS totalOtherAmounts,
SUM(a.service_amount) AS serviceAmounts,
SUM(a.tax_amount) AS taxAmounts,
SUM(a.total_amount) AS totalAmounts,
SUM(a.confirm_fund_amount) AS confirmFundAmounts,
SUM(a.total_insum_amount) AS totalInsumAmounts
from ims_invoice a
left join ims_customer_center b on a.cust_center_id = b.id
group by b.id, month_group;