SQL 日期范围

发布于 2024-09-28 13:17:22 字数 626 浏览 0 评论 0原文

可能的重复:
使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-10-05 13:17:22

如果您不使用 SQL Server,则应该查看 DATEDIFF 函数或等效函数。

MSDN 上的 DATEDIFF

You should look at the DATEDIFF function or equivalent if you're not using SQL Server.

DATEDIFF on MSDN

梦晓ヶ微光ヅ倾城 2024-10-05 13:17:22

如果您从应用程序传递日期参数,请考虑修改应用程序以执行日期范围计算。通过传入两个参数,您可以减轻 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文