我正在尝试在SQL Server中创建函数,该函数返回传递给函数的日期范围之间的月份。以下是我目前的脚本 -
CREATE FUNCTION [dbo].[fn_get_date_range] (
@startDate datetime,
@endDate datetime,
@frequency varchar(20)
)
RETURNS
@dateRanges TABLE
(
report_date datetime
)
AS
BEGIN
WITH DateRange (report_date) AS
(
SELECT row_number() OVER (ORDER BY object_id) FROM sys.all_objects)
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@StartDate)+report_date,0)
) AS report_date
insert into @dateRanges (report_date)
Select report_date FROM DateRange
WHERE report_date <= DATEDIFF(month, @StartDate, @EndDate)+1
RETURN
END
但是当我尝试创建这些函数时,我得到以下错误 -
已定义但未使用的公用表表达式。
我尝试了不同的方法,但它没有用。任何正确方向的指针都将受到赞赏。谢谢。
答案 0 :(得分:1)
不允许CTE没有跟SELECT
声明,MS引用:
•CTE必须后跟一个SELECT语句。 INSERT,UPDATE, 不支持DELETE和MERGE语句。
<强>更新强>
CREATE FUNCTION [dbo].[fn_get_date_range] (
@startDate datetime,
@endDate datetime,
@frequency varchar(20)
)
RETURNS
@dateRanges TABLE
(
report_date datetime
)
AS
BEGIN
WITH DateRange (report_date) AS
(
SELECT row_number() OVER (ORDER BY object_id) FROM sys.all_objects)
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@StartDate)+report_date,0)
) AS report_date
Select * FROM DateRange
INTO #temp_table
WHERE report_date <= DATEDIFF(month, @StartDate, @EndDate)+1
insert into @dateRanges (report_date)
select * from #temp_table
drop table #temp_table
RETURN
END
答案 1 :(得分:1)
这是我用于动态日期时间范围的TVF。
与您的方法类似,但我确实将DATEPART作为参数和增量。
示例 - 月份增量1:
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
<强>返回强>
示例 - 分钟增量15:
Select * from [dbo].[tvf-Range-Date]('2017-01-01','2017-01-02','MI',15)
<强>返回强>
感兴趣的UDF
CREATE FUNCTION [dbo].[tvf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )
Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[tvf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
编辑 - 按要求
CREATE FUNCTION [dbo].[tvf-Range-Date-Span] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a,cte1 b,cte1 c,cte1 d,cte1 e,cte1 f,cte1 g,cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY,N*@Incr,@R1) When 'QQ' then DateAdd(QQ,N*@Incr,@R1) When 'MM' then DateAdd(MM,N*@Incr,@R1) When 'WK' then DateAdd(WK,N*@Incr,@R1) When 'DD' then DateAdd(DD,N*@Incr,@R1) When 'HH' then DateAdd(HH,N*@Incr,@R1) When 'MI' then DateAdd(MI,N*@Incr,@R1) When 'SS' then DateAdd(SS,N*@Incr,@R1) End From cte2 )
Select RetSeq = N+1
,RetVal1 = D
,RetVal2 = LEAD(D,1,@R2) over (Order By D)
From cte3,cte0
Where N<cte0.M-1
)
--Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
--Select * from [dbo].[tvf-Range-Date-Span]('2016-10-01','2020-10-01','YY',1)
返回类似
的内容编辑2 - 2008年
;with cte as (
Select * from [dbo].[tvf-Range-Date]('2016-01-01','2017-01-01','MM',1)
)
Select DateR1 = A.RetVal
,DateR2 = B.NxtDate
From cte A
Cross Apply (Select NxtDate=min(RetVal) from cte where RetVal > A.RetVal ) B
Where B.NxtDate is not null
<强>返回强>
答案 2 :(得分:1)
这是与John的功能类似的变体......
CREATE FUNCTION dbo.tfn_BuildDateRanges
(
@startDate DATETIME,
@endDate DATETIME,
@dateInterval VARCHAR(20),
@interval INT
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b),
cte_Calendar AS (
SELECT TOP ((CASE
WHEN @dateInterval IN ('year', 'yy', 'yyyy') THEN DATEDIFF(year, @startDate, @endDate)
WHEN @dateInterval IN ('quarter', 'qq', 'q') THEN DATEDIFF(quarter, @startDate, @endDate)
WHEN @dateInterval IN ('month', 'mm', 'm') THEN DATEDIFF(month, @startDate, @endDate)
WHEN @dateInterval IN ('week wk, ww') THEN DATEDIFF(week, @startDate, @endDate)
WHEN @dateInterval IN ('day', 'dd', 'd') THEN DATEDIFF(day, @startDate, @endDate)
WHEN @dateInterval IN ('hour', 'hh') THEN DATEDIFF(hour, @startDate, @endDate)
WHEN @dateInterval IN ('minute', 'mi', 'n') THEN DATEDIFF(minute, @startDate, @endDate)
WHEN @dateInterval IN ('second', 'ss', 's') THEN DATEDIFF(second, @startDate, @endDate)
END / @interval + 1))
BegOfRange = CASE
WHEN @dateInterval IN ('year', 'yy', 'yyyy') THEN DATEADD(year, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('quarter', 'qq', 'q') THEN DATEADD(quarter, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('month', 'mm', 'm') THEN DATEADD(month, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('week wk, ww') THEN DATEADD(week, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('day', 'dd', 'd') THEN DATEADD(day, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('hour', 'hh') THEN DATEADD(hour, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('minute', 'mi', 'n') THEN DATEADD(minute, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
WHEN @dateInterval IN ('second', 'ss', 's') THEN DATEADD(second, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1) * @interval, @startDate)
END,
EndOfRange = CASE
WHEN @dateInterval IN ('year', 'yy', 'yyyy') THEN DATEADD(year, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('quarter', 'qq', 'q') THEN DATEADD(quarter, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('month', 'mm', 'm') THEN DATEADD(month, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('week wk, ww') THEN DATEADD(week, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('day', 'dd', 'd') THEN DATEADD(day, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('hour', 'hh') THEN DATEADD(hour, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('minute', 'mi', 'n') THEN DATEADD(minute, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
WHEN @dateInterval IN ('second', 'ss', 's') THEN DATEADD(second, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ) * @interval, @startDate)
END
FROM
cte_n3 a CROSS JOIN cte_n3 b
)
SELECT
c.BegOfRange,
c.EndOfRange
FROM
cte_Calendar c;
GO