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

后端 未结 5 2106
没有蜡笔的小新
没有蜡笔的小新 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:08

    I came researching the options that I would have to do this, however, I believe the method I use is the simplest:

    SELECT COUNT(*), 
           DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup 
    FROM TABLE 
    GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0) 
    ORDER BY dtgroup ASC;
    

提交回复
热议问题