我有一堆产品订单,我正在尝试按日期分组,并将该日期的数量相加。如何在不考虑时间的情况下按月/日/年进行分组?
3/8/2010 7:42:00
应与3/8/2010 4:15:00
答案 0 :(得分:314)
Cast/Convert您的论坛的Date
类型值。
GROUP BY CAST(myDateTime AS DATE)
答案 1 :(得分:24)
GROUP BY DATEADD(day, DATEDIFF(day, 0, MyDateTimeColumn), 0)
或者在 SQL Server 2008 之后,您可以简单地转换为Date
@Oded建议:
GROUP BY CAST(orderDate AS DATE)
答案 2 :(得分:16)
在pre Sql 2008中取出日期部分:
GROUP BY CONVERT(CHAR(8),DateTimeColumn,10)
答案 3 :(得分:4)
@SpringBootApplication
@ComponentScan({ "your controllers packages here", "other packages if you use them" })
@EnableJpaRepositories(basePackages = "your repo packages here")
@EntityScan(basePackages = "your entities")
答案 4 :(得分:2)
这是我在需要计算没有时间部分的特定日期的记录数时使用的示例:
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)
答案 5 :(得分:2)
迄今为止的CAST日期时间字段
export { assert } from "https://deno.land/std@v0.39.0/testing/asserts.ts";
export { green, bold } from "https://deno.land/std@v0.39.0/fmt/colors.ts";
答案 6 :(得分:0)
这个示例在oracle中运行良好
select to_char(columnname, 'DD/MON/yyyy'), count(*) from table_name group by to_char(createddate, 'DD/MON/yyyy');
答案 7 :(得分:0)
嗯,对我来说这很直接,我使用了带 groupby 的演员表:
示例:
Select cast(created_at as date), count(1) from dbname.tablename GROUP BY cast(created_at as date)
注意:我在 MSSQL 2016 上使用它。