Mysql
 sql >> Datenbank >  >> RDS >> Mysql

SQL, wie nach Geschäftsquartal und Jahr mit einem Datumsfeld gruppiert wird

So etwas sollte funktionieren:

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
;