简体   繁体   English

使用SQL Server的90天范围

[英]90 days range using SQL server

is there a way in SQL server that i can count the items 90 days prior to the inception date and the items 90 days after the inception date. 在SQL Server中有一种方法可以让我计算开始日期之前90天的项目和开始日期之后90天的项目。 Example: 例:

select
site,
count(*)
from mytable
where date >=10/1/2009' 
and date <'12/30/2009'
group by site

90 days before - after inception date.
prior to inception date = 7/3/2009.
inception date = 10/1/2009.
after inception date = 12/29/2009.

Use: 采用:

  SELECT t.site,
         SUM(CASE WHEN t.date BETWEEN DATEADD(dd, -90, '2009-10-01') AND DATEADD(ss, -1, '2009-10-01') THEN 1 ELSE 0 END) AS numPrior,
         SUM(CASE WHEN t.date BETWEEN DATEADD(dd, 1, '2009-10-01') AND DATEADD(dd, 91, '2009-10-01') THEN 1 ELSE 0 END) AS numPost
    FROM YOUR_TABLE t
GROUP BY t.site

Tweak the DATEADD function and inception date as you need. 根据需要调整DATEADD函数和起始日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM