これは、少しグーグルで調べた後に行ったことです。もっと効率的な方法を考えていましたが、これで十分だと思います。最初の部分は日付テーブルの入力で、2 番目の部分は四半期範囲の計算です。
DECLARE @StartDate SMALLDATETIME
DECLARE @EndDate SMALLDATETIME
SET @StartDate = '1/1/2011'
SET @EndDate = '12/31/2011'
-- creates a date table, not needed if there is one already
DECLARE @date TABLE ( [date] SMALLDATETIME )
DECLARE @offset INT
SET @offset = 0
WHILE ( @offset < DATEDIFF(dd, @StartDate, DATEADD(dd, 1, @EndDate)) )
BEGIN
INSERT INTO @date ( [date] )
VALUES ( DATEADD(dd, @offset, @StartDate) )
SELECT @offset = @offset + 1
END ;
WITH dateCTE
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY [date] ASC ) AS qID ,
[date] AS qStart ,
CASE WHEN DATEADD(dd, -1, DATEADD(q, 1, [date])) > @EndDate
THEN @EndDate
ELSE DATEADD(dd, -1, DATEADD(q, 1, [date]))
END AS qEnd
FROM @date
WHERE [date] = @StartDate
OR ( DATEDIFF(mm, @StartDate, [date]) % 3 = 0
AND DATEPART(dd, [date]) = DATEPART(dd,
@StartDate)
)
)