Teradata有条件扩展

时间:2014-03-01 16:38:04

标签: sql teradata

我有一个日期和val的表格,我试图扩展并按顺序填写缺少的日期。没有显示我是通过小组和位置来做这件事,但我需要做的关键在于下面。说我有下表

dt | val
2014-01-01 | 10
2014-02-17  | 9 
2014-04-21  | 5

我已经扩展到这是一个填充缺少零周的表格

week_bgn_dt| week_end_dt|  val
2014-01-01 | 2014-01-08 |  10
2014-01-09 | 2014-01-16 |  0
2014-01-17 | 2014-01-24 |  0
...
2014-02-10 | 2014-02-17 |  0
2014-02-18 | 2014-02-25 |  9
2014-02-26 | 2014-03-05 |  0
2014-03-06 | 2014-03-13 |  0
...
2014-03-30 | 2014-04-06 |  0
2014-04-07 | 2014-04-14 |  0
2014-04-15 | 2014-04-22 |  5

我想要的是填写最后一个值,直到更改,因此输出看起来像

week_bgn_dt| week_end_dt|  val
2014-01-01 | 2014-01-08 |  10
2014-01-09 | 2014-01-16 |  10
2014-01-17 | 2014-01-24 |  10
...
2014-02-10 | 2014-02-17 |  10
2014-02-18 | 2014-02-25 |  9
2014-02-26 | 2014-03-05 |  9
2014-03-06 | 2014-03-13 |  9
...
2014-03-30 | 2014-04-06 |  9
2014-04-07 | 2014-04-14 |  9
2014-04-15 | 2014-04-22 |  5

在teradata我试过这个

case when val <> 0 then val 
   else sum(val) over (partition by group, location order by group, store, week_bgn_dt 1 preceding  to current row) as val2

但这只会给出最后一次值,就像这样,

week_bgn_dt| week_end_dt|  val | val2
2014-01-01 | 2014-01-08 |  10  | 10
2014-01-09 | 2014-01-16 |  0   | 10
2014-01-17 | 2014-01-24 |  0   | 0
...
2014-02-10 | 2014-02-17 |  0   | 0 
2014-02-18 | 2014-02-25 |  9   | 9
2014-02-26 | 2014-03-05 |  0   | 9
2014-03-06 | 2014-03-13 |  0   | 0
...
2014-03-30 | 2014-04-06 |  0   | 0
2014-04-07 | 2014-04-14 |  0   | 0
2014-04-15 | 2014-04-22 |  5   | 5

如果我使窗口无限制,那么当我点击一个新值

时它会合计
case when val <> 0 then val 
   else sum(val) over (partition by group, location order by group, store, week_bgn_dt unbounded preceding  to current row) as val2

week_bgn_dt| week_end_dt|  val | val2
2014-01-01 | 2014-01-08 |  10  | 10
2014-01-09 | 2014-01-16 |  0   | 10
2014-01-17 | 2014-01-24 |  0   | 10
...
2014-02-10 | 2014-02-17 |  0   | 10 
2014-02-18 | 2014-02-25 |  9   | 9
2014-02-26 | 2014-03-05 |  0   | 19
2014-03-06 | 2014-03-13 |  0   | 19
...
2014-03-30 | 2014-04-06 |  0   | 19
2014-04-07 | 2014-04-14 |  0   | 19
2014-04-15 | 2014-04-22 |  5   | 5

我尝试过max()和min(),但结果相似。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

这似乎是SUM操作中的分区问题。请记住,当指定OVER子句时,SUM将为每个分区分别计算每个分区的结果。您希望SUM能够在多个分区上运行。由于我们无法以任何方式告诉SUM(我知道)在多个分区上运行,因此可以将分区重新定义为其他分区。

我的情况是,SUM似乎根本不应该使用分区。我们所需要的只是RESET WHEN功能和OVER的窗口操作。使用填充零的扩展结果,我已通过以下查询获得了所需的输出。

SELECT 
    week_bgn_dt,
    week_end_dt,
    val,
    SUM(val) OVER ( PARTITION BY 1
                    ORDER BY location ASC, week_bgn_dt ASC
                    RESET WHEN val<>0
                    ROWS UNBOUNDED PRECEDING ) AS val2
FROM test

week_bgn_dt | week_end_dt | val | val2
2014-01-01  | 2014-01-08  |  10 |  10
2014-01-09  | 2014-01-16  |  0  |  10
2014-01-17  | 2014-01-24  |  0  |  10
2014-02-10  | 2014-02-17  |  0  |  10
2014-02-18  | 2014-02-25  |  9  |  9
2014-02-26  | 2014-03-05  |  0  |  9
2014-03-06  | 2014-03-13  |  0  |  9
2014-03-30  | 2014-04-06  |  0  |  9
2014-04-07  | 2014-04-14  |  0  |  9
2014-04-15  | 2014-04-22  |  5  |  5

您可能已经注意到我只在提供的数据中添加了位置。我相信您可以将其余字段添加到ORDER BY子句并获得正确的结果。