开发者

MySQL offset date in group by

I have the following query which is pulling data fine. It currently gro开发者_运维知识库ups data by month and then year. I am trying to group the monthly data from the 19th of one month to the 18th of the next so that it fits in with our financial months as opposed to calander months. Is this possible?

SELECT
    sum(invoice.invoice_amount_payable)AS invoice_totals,
    company.company_label AS company_label,
    MONTHNAME(invoice.invoice_date_paid)AS MONTH,
    YEAR(invoice.invoice_date_paid)AS YEAR
FROM
    invoice
INNER JOIN company ON invoice.company_id = company.company_id
WHERE
    invoice.invoice_active = 1
AND invoice.invoice_status_id = 7
AND invoice.invoice_date_deleted = 0
AND invoice.invoice_date_paid >= '2009-1-1'
AND invoice.invoice_date_paid <= '2011-8-20'
GROUP BY
    MONTH(invoice.invoice_date_paid),
    YEAR(invoice.invoice_date_paid)
ORDER BY
    YEAR(invoice.invoice_date_paid),
    MONTH(invoice.invoice_date_paid)


To use the month and year functions of mysql you could simply shift your invoice day by the required offset like:

invoice_date_paid + INTERVAL 19 DAY
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜