我正在尝试创建一个脚本(使用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列的日期部分。它正在计算总数。感谢您提供的任何帮助!
答案 0 :(得分:1)
select Convert(date,date_published), count(customer_id)
from CustomerTable
WHERE datediff(d,getdate(),date_published )>-7
GROUP BY Convert(date,date_published)
答案 1 :(得分:1)
尝试将GROUP BY date_published
修改为:
GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, date_published))
答案 2 :(得分: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)