SQL grouping by month and year

后端 未结 9 1758
旧时难觅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条回答
  •  误落风尘
    2020-11-27 16:21

    If you want to stay having the field in datetime datatype, try using this:

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

    It it also easy to change to group by hours, days, weeks, years...
    I hope it is of use to someone,

    Regards!

提交回复
热议问题