1

ETL Question here.

For a given table that includes entries with a start and end date, what is the optimal method to retrieve counts for each day, including those days that may not have an entry within the scope of the start end date.

Given Table Example

Stock
ID     StartDate   EndDate     Category
1      1/1/2013    1/5/2013    Appliances
2      1/1/2013    1/10/2013   Appliances 
3      1/2/2013    1/10/2013   Appliances

Output required

Available
Category     EventDate      Count
Appliances   1/1/2013  2
Appliances   1/2/2013  3
...
...
Appliances   1/10/2013 2
Appliances   1/11/2013 0
...
...

One method I know of, which takes FOREVER, is to create a Table variable, and run a While Block iterating through the start and end of the range I wish to retrieve, then execute a query like so..

Insert into @TempTable (Category,EventDate,Count) 
FROM Stock 
Where @CurrentLoopDate BETWEEN StartDate AND EndDate

Another method would be to create a table or temp table of dates in the range I want populated, and join it with a BETWEEN function.

Insert into @TempTable (Category,EventDate,Count) 
FROM DateTable
   INNER JOIN Stock  ON DateTable.[Date] BETWEEN StartDate AND EndDate

Yet other methods are similar but use SSIS, but essentially are the same as the above two solutions.

Any GURU's know of a more efficient method?

4

1 回答 1

2

您是否尝试过使用递归 CTE?

WITH Dates_CTE AS (
SELECT [ID]
      ,[StartDate]
      ,[EndDate]
      ,[Category]
FROM [dbo].[Stock]
UNION ALL
SELECT   [ID]
        ,DATEADD(D, 1, [StartDate])
        ,[EndDate]
        ,[Category]
FROM Dates_cte d
WHERE DATEADD(D, 1, [StartDate]) <= EndDate
)

SELECT   StartDate AS EventDate
    ,Category
    ,COUNT(*)
FROM Dates_CTE
GROUP BY StartDate, Category
OPTION (MAXRECURSION 0)

这应该够了吧 ;-)

于 2014-04-16T12:56:34.707 回答