SQL query to retrieve financial year data grouped by the year

前端 未结 3 1731
一整个雨季
一整个雨季 2021-02-10 07:52

I have a database with lets assume two columns (service_date & invoice_amount). I would like to create an SQL query that would retrieve and group the data for each financial

相关标签:
3条回答
  • 2021-02-10 08:23
    SELECT
       CASE WHEN MONTH(service_date)>=7 THEN
              concat(YEAR(service_date), '-',YEAR(service_date)+1)
       ELSE concat(YEAR(service_date)-1,'-', YEAR(service_date)) END AS financial_year,
       SUM(invoice_amount)
    FROM mytable
    GROUP BY financial_year
    

    which produce o/p like below

    financial_year   invoice_amount
    2007-2008        10000.00
    2008-2009        15000.00
    2009-2010        20000.00
    
    0 讨论(0)
  • 2021-02-10 08:34
    to get data from April to March  ---
    
    SELECT
    CASE 
        WHEN MONTH(application_receiving_date)>=4 
        THEN concat(YEAR(application_receiving_date), '-',YEAR(application_receiving_date)+1) 
        ELSE concat(YEAR(application_receiving_date)-1,'-', YEAR(application_receiving_date)) 
    END AS app_year,
    from table_name 
    GROUP BY app_year  
    
    0 讨论(0)
  • 2021-02-10 08:48

    This works for me in MSSQL.. Hope it works for you in MySQL

    SELECT
       CASE WHEN MONTH(service_date)>=7 THEN
               YEAR(service_date)+1 
       ELSE YEAR(service_date) END AS financial_year,
       SUM(invoice_amount)
    FROM mytable
    GROUP BY
       CASE WHEN MONTH(service_date)>=7 THEN
               YEAR(service_date)+1 
       ELSE YEAR(service_date)
    
    0 讨论(0)
提交回复
热议问题