我有一堆产品订单,我试图按日期分组,并合计该日期的数量。我如何按月/日/年分组而不考虑时间部分?
3/8/2010 7:42:00应该和3/8/2010 4:15:00组合在一起
3/8/2010 7:42:00
3/8/2010 4:15:00
GROUP BY DATEADD(day, DATEDIFF(day, 0, MyDateTimeColumn), 0)
或者在SQL Server 2008之后,你可以像@Oded建议的那样简单地转换为Date:
Date
GROUP BY CAST(orderDate AS DATE)
演员/转换将组的值转换为Date类型。
GROUP BY CAST(myDateTime AS DATE)
在Sql 2008之前,通过去掉日期部分:
GROUP BY CONVERT(CHAR(8),DateTimeColumn,10)
下面是我使用的一个例子,当我需要计算特定日期的记录数量而不包括时间部分时:
select count(convert(CHAR(10), dtcreatedate, 103) ),convert(char(10), dtcreatedate, 103) FROM dbo.tbltobecounted GROUP BY CONVERT(CHAR(10),dtcreatedate,103) ORDER BY CONVERT(CHAR(10),dtcreatedate,103)
下面的例子在oracle中工作得很好
select to_char(columnname, 'DD/MON/yyyy'), count(*) from table_name group by to_char(createddate, 'DD/MON/yyyy');
GROUP BY DATE(date_time_column)
Group By DATE_TRUNC('day' , 'occurred_at_time')
CAST datetime字段到日期
select CAST(datetime_field as DATE), count(*) as count from table group by CAST(datetime_field as DATE);
好吧,对我来说,它几乎是直接的,我使用cast和groupby:
例子:
Select cast(created_at as date), count(1) from dbname.tablename GROUP BY cast(created_at as date)
注意:我在MSSQL 2016上使用这个。