SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value

后端 未结 5 2150
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 11:54

I have a table with two fields - datetime and int. I want to do a group by on the datetime only on the date ignoring the hour and minute.

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 12:05

    Personally i prefer the format function, allows you to simply change the date part very easily.

         declare @format varchar(100) = 'yyyy/MM/dd'
         select 
            format(the_date,@format), 
            sum(myfield) 
         from mytable 
         group by format(the_date,@format) 
         order by format(the_date,@format) desc;
    

提交回复
热议问题