3

我正在尝试创建一个脚本(使用 MS SQL Server 2008 R2),它可以计算上周每天添加到表中的总行数。我使用以下脚本来执行此操作:

select date_published, count(customer_id)
from CustomerTable
WHERE date_published >=dateadd(day,datediff(day,0,GetDate())- 7,0)
GROUP BY date_published

这个脚本有效,但我遇到的问题是我的表中的 date_published 列也需要包含时间,所以我需要弄清楚如何让脚本在计算时只查看 date_published 列的日期部分总数。感谢您的任何帮助,您可以提供!

4

3 回答 3

1

尝试修改您GROUP BY date_published的:

GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, date_published))
于 2012-09-21T14:25:28.163 回答
1
select Convert(date,date_published), count(customer_id) 
from CustomerTable 
WHERE datediff(d,getdate(),date_published )>-7
GROUP BY Convert(date,date_published)
于 2012-09-21T14:23:52.977 回答
0
select cast(date_published as date) as date_published,
       count(customer_id) as count_customer_id
from CustomerTable
where date_published >= dateadd(day, -7, cast(getdate() as date))
group by cast(date_published as date)
于 2012-09-22T09:18:23.757 回答