Mysql
 sql >> Base de données >  >> RDS >> Mysql

SQL comment regrouper par trimestre fiscal et année avec un champ de date

Quelque chose comme ceci devrait fonctionner :

SELECT 
    YEAR([Expected Arrival Date]) + CASE
        WHEN MONTH([Expected Arrival Date]) IN (11, 12) THEN 1
        ELSE 0
    END As FiscalYear,
    CASE
        WHEN MONTH([Expected Arrival Date]) IN (11, 12, 1) THEN 1
        WHEN MONTH([Expected Arrival Date]) IN (2, 3, 4) THEN 2
        WHEN MONTH([Expected Arrival Date]) IN (5, 6, 7) THEN 3
        ELSE 4
    END As FiscalQuarter,
    ...
GROUP BY
    YEAR([Expected Arrival Date]) + CASE
        WHEN MONTH([Expected Arrival Date]) IN (11, 12) THEN 1
        ELSE 0
    END,
    CASE
        WHEN MONTH([Expected Arrival Date]) IN (11, 12, 1) THEN 1
        WHEN MONTH([Expected Arrival Date]) IN (2, 3, 4) THEN 2
        WHEN MONTH([Expected Arrival Date]) IN (5, 6, 7) THEN 3
        ELSE 4
    END,
    [Vendor Name]
ORDER BY
    FiscalYear,
    FiscalQuarter
;