SQL 日期范围
可能的重复:
使用 SQL Server 的 90 天范围
我正在尝试获取 90 天前的计数至运行日期以及运行日期后 90 天的计数。例如,我的运营日期是 4/1/2004。因此,4/1/2004 之前的 90 天是(1/2/2004 到 3/31/2004),之后 90 天(包括 4/1/2004)是 6/29/2004。
我使用以下脚本并手动计算天数,这效率不高......
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
Possible Duplicate:
90 days range using SQL server
I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.
I used the following scripts and mannually calculate the days, which is not efficient...
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不使用 SQL Server,则应该查看 DATEDIFF 函数或等效函数。
MSDN 上的 DATEDIFF
You should look at the DATEDIFF function or equivalent if you're not using SQL Server.
DATEDIFF on MSDN
如果您从应用程序传递日期参数,请考虑修改应用程序以执行日期范围计算。通过传入两个参数,您可以减轻 SQL 的计算负担,从而提高性能。
If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.