我有一个包含两个字段的表-datetime
和int
。我想datetime
只在忽略小时和分钟的日期进行分组。该SELECT
语句应返回一个映射到一天的int之和的日期。
Answers:
SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumField
FROM MyTable
GROUP BY CAST(Datetimefield AS DATE)
select convert(convert(int, getdate()), datetime)
失败的错误语法错误
由于他没有指定使用的SQL Server版本(date
2005年不提供该类型),因此也可以使用
SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summed
FROM table_name
GROUP BY CONVERT(VARCHAR(10),date_column,112)
DATEADD(dd, DATEDIFF(dd, 0, date_field),0)
我来研究我必须要做的选择,但是,我相信我使用的方法是最简单的:
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;
-我喜欢这样,因为数据类型和格式与日期时间数据类型保持一致
;with cte as(
select
cast(utcdate as date) UtcDay, DATEPART(hour, utcdate) UtcHour, count(*) as Counts
from dbo.mytable cd
where utcdate between '2014-01-14' and '2014-01-15'
group by
cast(utcdate as date), DATEPART(hour, utcdate)
)
select dateadd(hour, utchour, cast(utcday as datetime)) as UTCDateHour, Counts
from cte