SQL Select语句中的索引

时间:2014-01-07 19:09:30

标签: sql sql-server indexing

目前有一个脚本可以选择一年中的前5个月。

...
;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
),
...

如何修改此脚本只需要1,2,4,5个月。

1 个答案:

答案 0 :(得分:3)

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