Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

SQL como agrupar por trimestre fiscal e ano com um campo de data


Algo assim deve funcionar:
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
;