SQL grouping by month and year

后端 未结 9 1732
旧时难觅i
旧时难觅i 2020-11-27 15:57

I\'m not sure what should I write in the following SQL query to show \'date\' column like this: \"month-year\" - \"9-2011\".

SELECT MONTH(date) + \'.\' + YE         


        
9条回答
  •  Happy的楠姐
    2020-11-27 16:22

    If I understand correctly. In order to group your results as requested, your Group By clause needs to have the same expression as your select statement.

    GROUP BY MONTH(date) + '.' + YEAR(date)
    

    To display the date as "month-date" format change the '.' to '-' The full syntax would be something like this.

    SELECT MONTH(date) + '-' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS
    SumaMarketing, SUM(revenue) AS SumaZarada 
    FROM [Order]
    WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
    GROUP BY MONTH(date) + '.' + YEAR(date)
    

提交回复
热议问题