Antarr Byrd
Antarr Byrd

Reputation: 26091

Indexing in SQL Select Statement

Currently have a script that select the first 5 months of the year like this.

...
;WITH months AS 
(
  -- we need 5 months
  SELECT TOP (5) m = number FROM master.dbo.spt_values 
  WHERE type = 'P' ORDER BY number
),
-- we need a week in each month, starting at the 1st
weeks AS (SELECT w FROM (VALUES(0),(1),(2),(3)) AS w(w)),
dates AS 
(
  -- this produces a date for the first 4 weeks of each
  -- month from the start date
  SELECT d = DATEADD(WEEK,w.w,DATEADD(MONTH,m.m,@start)) 
  FROM months AS m CROSS JOIN weeks AS w
),
...

How can I modify this script to take months 1,2,4,5 only.

Upvotes: 0

Views: 65

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280350

FROM months AS m CROSS JOIN weeks AS w
WHERE m.m IN (0,1,3,4)

Upvotes: 3

Related Questions