Question

I have created a table-valued function that can be called up to provide results in this way:

Select @DateId as DateId,*
From dbo.PastDueARcalc(@DateId)
Where PastDueAR <> 0
and CustomerId in (16629, 9969, 19809)

resulting in something like this:

DateId      customerid  IDNId       AccountTypeId RegionId    BusinessUnitId PastDueAR
----------- ----------- ----------- ------------- ----------- -------------- ----------           
41657       16629       2949        1             3           2              7418.97
41657       9969        8233        1             5           3              5188.47
41657       19809       9522        1             5           7              15.82
...

So far, so good... now here is the kicker. I'm trying to build an SSRS chart that will give me the SUM(PastDueAR). All other fields are parameter filters for the report

But as you can see, I only have one day's worth of data. That is all I get from the function.

I need loop through the function with all the values from the parameters @StartDate and @EndDate selected by the user. And union all of those together. Ultimately, the report can have values= SUM(PastDueAR) and groups = DateId

Was it helpful?

Solution

You will need to somehow generate the list of all dates you are interested in, then CROSS APPLY against your table-valued function.

For example (tweak based on the specifics of your use case):

DECLARE @dates TABLE (dt datetime)

-- you can get the dates into the table in any way that is most appropriate...
INSERT INTO @dates (dt) VALUES ('1/1/2014')
INSERT INTO @dates (dt) VALUES ('1/2/2014')
INSERT INTO @dates (dt) VALUES ('1/3/2014')

SELECT SUM(pdar.PastDueAR) AS PastDueAR_Total
FROM @dates dates
    CROSS APPLY dbo.PastDueARcalc(dates.dt) pdar
WHERE pdar.PastDueAR <> 0
AND CustomerId in (16629, 9969, 19809)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top