使用生产日期表添加缺少的行

时间:2012-06-20 15:49:28

标签: sql sql-server tsql

此问题与this SO post

有关

如何使用递归CTE而不是使用DimDates表添加缺失数据(通过日期视为缺失)?

我有以下两个表:

create table the_table 
(
  [Date] datetime,
  Category2 varchar(10),
  Amount INT
)
insert into the_table
values
( '01 jan 2012', 'xx', 10),
( '03 jan 2012', 'yy', 50)


create table DimDate 
(
  [Date] datetime
)
insert into DimDate
values
( '01 jan 2012'),
( '02 jan 2012'),
( '03 jan 2012'),
( '04 jan 2012')

这些是我想要达到的结果。我没有为递归CTE烦恼,因为我错误地认为,使用我们的仓库DimDate表可以更轻松地加载:

enter image description here

好吧 - 我可能偶然发现了一个可能的解决方案 - 如果错误的话,请在下面挖洞:

select

  coalesce(x.[Date], y.[Date]) AS Date ,
  coalesce(x.Category2, y.Category2) AS Category2 ,
  isnull(Amount,0) as Amount
from the_table x
full outer join 
(
select 
    d.Date
    , t.Category2
from 
        the_table t
        cross join DimDate d 
) y
    on
    x.Category2 = y.Category2
    and 
    x.Date = y.Date

这就是我最终的结果。结合了明显的答案和Aaron的帖子中的cte:

;WITH 
    Dates_cte ([Date]) AS
            (
            SELECT [Date] = DayMarker 
            FROM WHData.dbo.vw_DimDate x
            WHERE
                    x.DayMarker >= (SELECT MIN([Date]) FROM #Data1 WHERE Period = 'Daily') AND
                    x.DayMarker <= GETDATE()
            )   
    ,Categories ([Operator], [Market], [Product], [Measure]) AS 
                ( 
                SELECT DISTINCT 
                        [Operator]
                        , [Market]
                        , [Product]
                        , [Measure] 
                FROM #Data1 
                WHERE [Period] = 'Daily'
                ) 
INSERT INTO #Data1 
    SELECT 
         c.[Operator]
        , c.[Market]
        , c.[Product]
        , [Period] = CONVERT(VARCHAR(100), 'Daily')
        , d.[Date]  
        , c.[Measure]   
        , 0 
    FROM Dates_cte d CROSS JOIN Categories c
    WHERE NOT EXISTS 
            ( 
            SELECT * 
            FROM #Data1 AS T 
            WHERE 
                    t.[Period] = 'Daily' AND
                    t.[Operator] = c.[Operator] AND 
                    t.[Market] = c.[Market] AND 
                    t.[Product] = c.[Product] AND 
                    t.[Measure] = c.[Measure] AND 
                    t.[Date] = d.[Date] 
            ) 

4 个答案:

答案 0 :(得分:3)

使用INSERT INTO ... SELECT FROM DimDate CROSS JOIN categories WHERE NOT EXISTS ...

试试这个:

INSERT INTO the_table
([Date], Category2, Amount)
SELECT [Date], category2, 0
FROM DimDate
CROSS JOIN
(
    SELECT DISTINCT category2 FROM the_table
) AS categories
WHERE NOT EXISTS
(
    SELECT *
    FROM thetable AS T
    WHERE T.category2 = categories.Category2
    AND T.[Date] = DimDate.[Date]
)

查看在线工作:ideone

如果您正在创建数据仓库,我建议您将类别放入维度表中。

答案 1 :(得分:1)

显然是一种可能解决方案的错误伪代码

insert into table1
    select  from table2 
        where not exists (select from table1 where table1.date = table2.date)

假设您要将数据添加到表1中。

如果你只是想在内存中,

select * from table 1
union 
select * from table 2 where not exists (select from table1 where table1.date = table2.date)

或只是外部联接

答案 2 :(得分:1)

;WITH cat AS (SELECT Category2 FROM the_table GROUP BY Category2)
INSERT the_table([Date], Category2, Amount)
SELECT d.[Date], cat.Category2, 0
FROM DimDate AS d CROSS JOIN cat
LEFT OUTER JOIN the_table AS t
ON d.[Date] = t.[Date]
AND cat.Category2 = t.Category2
WHERE t.[Date] IS NULL;

答案 3 :(得分:0)

步骤1,插入缺少的日期:

select [Date], '', 0 from DimDate
where [Date] not in (select [Date] from the_table)

第2步,更新Categoriy2列:

update the_table
set Category2 =
     (select aux.Category from the_table aux where t.Date = 
        (select max(t.Date) from the_table t
         where t.Category2 <> '' and t.Date < aux.Date)