3

回顾这个SO 帖子

如果有一个分组类别“类别”,为简单起见,可以是 X 或 Y - 修改此脚本是否是一件小事,以便为每个类别添加缺失的日期?

我认为该类别需要添加到 CTE 中吗?

换句话说,如果我有以下初始表:

在此处输入图像描述

...我如何获得以下信息:

在此处输入图像描述

将很快上传我的尝试

我已经调用了初始表#x。我希望适应如下递归 CTE 查询以包含字段类别:

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)

;WITH times AS 
    ( 
    SELECT @MinDate dt , 1 depth 
    UNION ALL 
    SELECT 
            DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM times 
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES t
    LEFT OUTER JOIN #X x
        ON 
        t.dt = x.Date

好的 - 我已经绑定了一个 CROSS JOIN 但它错误地扩展了内容:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            c.Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
            CROSS JOIN #Cat c
                --ON c.Category IS NOT NULL
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES

这似乎工作正常:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES
4

2 回答 2

1

这样的事情应该可以解决问题:

declare @curDate datetime, @maxDate datetime
declare @count tinyint

select @curDate = convert(datetime, '20120101', 112), @maxDate = getdate()
select @count = 0

while @curDate < @maxDate
begin
  select @count = count(1) from tablename where Category = 'X' and convert(varchar(8), Date, 112) = convert(varchar(8), @curDate, 112)

  if @count > 0
  begin
    insert into tablename
    select 'X', @curDate, 0
  end

  select @curDate = dateadd(dd, 1, @curDate)
end
于 2012-06-14T10:41:40.390 回答
1

这是一个没有日历表的解决方案(这是生产中的必需品)。您可能在变量中有日期范围,或者您可能会从the_table.

编辑:将类别合并到日期范围生成中的较短版本

declare @startdate datetime = '2012-1-1'
declare @enddate datetime = '2012-1-5'

; with dates([date], category) as (
  select distinct @startdate, category
    from the_table
   union all
  select dateadd (day, 1, [date]), category
    from dates
   where [date] < @enddate
)
select dates.date, 
       dates.category, 
       isnull(the_table.amount, 0) Amount
  from dates
  left join the_table
    on dates.date = the_table.date
   and dates.category = the_table.category
 order by dates.category, dates.date
option (maxrecursion 0)

有现场测试@Sql Fiddle新的 Sql 小提琴

于 2012-06-14T11:07:24.610 回答