语句的类数组函数
Array like function for a statement
我有一个声明,returns 一个日期的数字。我想要做的是能够在一系列日期中执行语句并为每个日期获取一个值。
select dbo.GetItemMTDIssues(inmastx.fac, inmastx.fpartno, inmastx.frev, '6-01-2019')
as MTDiss from inmastx where fpartno='ANF-10-6313-102'
这就是我在当前报表中查找单个日期的结果。 6-01-2019
|MTDiss|
600
这是我想要的日期范围内的预期结果,例如 6-01-2019 - 6-05-2019
|MTDiss|
600
450
375
700
300
如果有帮助,也包括该功能。
CREATE FUNCTION [dbo].[GetItemMTDIssues]
(@fac char(20), @partno char(25), @rev char(3), @currentdate datetime)
returns numeric (15,5)
as
begin
declare @returnval as numeric (15,5)
set @returnval =
isnull(
(select sum(fQty)
from intran
where ftype = 'I'
and month(fdate) = month(@currentdate)
and year(fdate) = year(@currentdate)
and fac = @fac
and fpartno = @partno
and fcpartrev = @rev)
,0.0) * -1
return @returnval
end
您需要先创建范围,下面的 t-sql 将执行此操作。
declare @startDate datetime='6-01-2019'
declare @endDate datetime='6-05-2019'
;with DateRange as (
select @startDate [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@endDate
)
select * from DateRange
我们可以测试一下,看看结果,确认这是我们想要的范围。
note:if你需要按月或天数跳转,其他按天跳转你只需要更改DATEADD中的代码。
现在我们需要更新您的函数以获取范围的开始和结束并让它使用所有范围日期,我认为类似下面的内容会有所帮助:-
CREATE FUNCTION [GetItemMTDIssuesRange]
(
@fac char(20), @partno char(25), @rev char(3), @startDateRange datetime, @EndDateRange datetime
)
RETURNS TABLE
AS
RETURN
(
with DateRange as (
select @startDateRange [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@EndDateRange
)
--select * from DateRange
select (isnull(sum(fQty),0.0) * -1) MTDiss
from intran
inner join DateRange on year(fdate) = year(DateRange.[date]) and month(fdate) = month(DateRange.[date])
where ftype = 'I'
and fac = @fac
and fpartno = @partno
and fcpartrev = @rev
group by DateRange.[date]
)
GO
请查收。
如果您不想更改功能,下面的内容可能也会有所帮助:-
declare @startDate datetime='6-01-2019'
declare @endDate datetime='6-05-2019'
;with DateRange as (
select @startDate [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@endDate
)
select dbo.GetItemMTDIssues(inmastx.fac, inmastx.fpartno, inmastx.frev, DateRange.[date])
as MTDiss from inmastx,DateRange
where fpartno='ANF-10-6313-102'
我有一个声明,returns 一个日期的数字。我想要做的是能够在一系列日期中执行语句并为每个日期获取一个值。
select dbo.GetItemMTDIssues(inmastx.fac, inmastx.fpartno, inmastx.frev, '6-01-2019')
as MTDiss from inmastx where fpartno='ANF-10-6313-102'
这就是我在当前报表中查找单个日期的结果。 6-01-2019
|MTDiss|
600
这是我想要的日期范围内的预期结果,例如 6-01-2019 - 6-05-2019
|MTDiss|
600
450
375
700
300
如果有帮助,也包括该功能。
CREATE FUNCTION [dbo].[GetItemMTDIssues]
(@fac char(20), @partno char(25), @rev char(3), @currentdate datetime)
returns numeric (15,5)
as
begin
declare @returnval as numeric (15,5)
set @returnval =
isnull(
(select sum(fQty)
from intran
where ftype = 'I'
and month(fdate) = month(@currentdate)
and year(fdate) = year(@currentdate)
and fac = @fac
and fpartno = @partno
and fcpartrev = @rev)
,0.0) * -1
return @returnval
end
您需要先创建范围,下面的 t-sql 将执行此操作。
declare @startDate datetime='6-01-2019'
declare @endDate datetime='6-05-2019'
;with DateRange as (
select @startDate [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@endDate
)
select * from DateRange
我们可以测试一下,看看结果,确认这是我们想要的范围。 note:if你需要按月或天数跳转,其他按天跳转你只需要更改DATEADD中的代码。
现在我们需要更新您的函数以获取范围的开始和结束并让它使用所有范围日期,我认为类似下面的内容会有所帮助:-
CREATE FUNCTION [GetItemMTDIssuesRange]
(
@fac char(20), @partno char(25), @rev char(3), @startDateRange datetime, @EndDateRange datetime
)
RETURNS TABLE
AS
RETURN
(
with DateRange as (
select @startDateRange [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@EndDateRange
)
--select * from DateRange
select (isnull(sum(fQty),0.0) * -1) MTDiss
from intran
inner join DateRange on year(fdate) = year(DateRange.[date]) and month(fdate) = month(DateRange.[date])
where ftype = 'I'
and fac = @fac
and fpartno = @partno
and fcpartrev = @rev
group by DateRange.[date]
)
GO
请查收。
如果您不想更改功能,下面的内容可能也会有所帮助:-
declare @startDate datetime='6-01-2019'
declare @endDate datetime='6-05-2019'
;with DateRange as (
select @startDate [date]
union all
select DATEADD(day,1,[date]) [date] from DateRange where [date]<@endDate
)
select dbo.GetItemMTDIssues(inmastx.fac, inmastx.fpartno, inmastx.frev, DateRange.[date])
as MTDiss from inmastx,DateRange
where fpartno='ANF-10-6313-102'