90 days range using SQL server

Posted on

90 days range using SQL server – This article will take you through the common SQL errors that you might encounter while working with sql, sql-server,  tsql. The wrong arrangement of keywords will certainly cause an error, but wrongly arranged commands may also be an issue. SQL keyword errors occur when one of the words that the SQL query language reserves for its commands and clauses is misspelled. If the user wants to resolve all these reported errors, without finding the original one, what started as a simple typo, becomes a much bigger problem.

SQL Problem :

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. Example:

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.

Solution :


  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
GROUP BY t.site

Tweak the DATEADD function and inception date as you need.


Finding SQL syntax errors can be complicated, but there are some tips on how to make it a bit easier. Using the aforementioned Error List helps in a great way. It allows the user to check for errors while still writing the project, and avoid later searching through thousands lines of code.

Leave a Reply

Your email address will not be published. Required fields are marked *