【问题标题】:How to implement lag function in teradata.如何在 teradata 中实现延迟功能。
【发布时间】:2019-01-03 11:24:50
【问题描述】:

输入:

输出:

我想要如下图所示的输出。

在输出图像中,'behind' 中的 4 被评估为 tot_cnt-tot 和 'behind' 中的后续数字,例如:2 被评估为 lag(behind)-tot & 只要 'rank' 仍然存在同样,即使“后面”也应该保持不变。

谁能帮我在 teradata 中实现这个?

【问题讨论】:

  • 如果tot_cnt 在不同的行中有不同的值怎么办?
  • @trincot 我稍后会尝试检查,但首先我需要在这个..中使用滞后功能。
  • 也许我会等你先澄清这一点,然后再花时间研究它:)
  • @trincot tot_cnt 实际上是观察的总数
  • LAG 实现了对上一行数据的访问,但我看不出这与您的预期结果有何匹配。

标签: sql teradata lag


【解决方案1】:

你似乎想要:

select *, (select count(*) 
           from table t1 
           where t1.rank > t.rank
          ) as behind
from table t;

【讨论】:

    【解决方案2】:

    我会总结数据并做:

    select id, max(tot_cnt), max(tot),
           (max(tot_cnt) -
            sum(max(tot)) over (order by id rows between unbounded preceding and current row)
           ) as diff
    from t
    group by id;
    

    这为每个id 提供一行,这对我来说更有意义。如果你想要原始数据行(无论如何都是重复的),你可以join这个回到你的表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多