零售/批发数据库SQL

如何写这个SQL

有这样的数据 , T1是日期序列,每天都有,T2 有时候没有

T1                T2               T3 (=T1-T2)

2017-01-01         2017-01-01             0

2017-01-02         2017-01-02             0

2017-01-03         2017-01-03             0

2017-01-04                 null                null               

2017-01-05                 null                null               

2017-01-06                 null                null               

2017-01-07         2017-01-07            0

期望结果, T2 IS NULL 的时候,用最近的T2补

T1                T2                    T3

2017-01-01         2017-01-01       0

2017-01-02         2017-01-02       0

2017-01-03         2017-01-03        0

2017-01-04         2017-01-03        1

2017-01-05         2017-01-03        2

2017-01-06         2017-01-03        3

2017-01-07         2017-01-07         0

参与7

3同行回答

y5_sety5_set其它不告诉你
假设你的表名为t,执行下面的SQL,但是这个是三层嵌套,耗费比较大select T1,T2,value(T3,(T1-T2)) from (select tab1.t1 as t1,case when tab1.t2 is not null then tab1.t2 else (select max(tab2.t2) from t tab2 where tab2.t2 is not null and tab2.T1...显示全部

假设你的表名为t,执行下面的SQL,但是这个是三层嵌套,耗费比较大


select T1,T2,value(T3,(T1-T2)) from (select tab1.t1 as t1,case when tab1.t2 is not null then tab1.t2 else (select max(tab2.t2) from t tab2 where tab2.t2 is not null and tab2.T1

收起
互联网服务 · 2017-06-16
浏览1692
masalymasaly其它易诚
select t1,t2,coalesce(t2,max(t2) over(order by t1 asc)) t3 from tab1;显示全部

select t1,t2,coalesce(t2,max(t2) over(order by t1 asc)) t3 from tab1;

收起
软件开发 · 2017-06-20
浏览1702
richard_macyrichard_macy数据库管理员macys
这个SUBQUERY可以用,谢谢想找有没有OLAP function 可以实现的。显示全部

这个SUBQUERY可以用,谢谢

想找有没有OLAP function 可以实现的。

收起
零售/批发 · 2017-06-17
浏览1635

提问者

richard_macy
数据库管理员macys
擅长领域: 数据库服务器存储

相关问题

相关资料

相关文章

问题状态

  • 发布时间:2017-06-16
  • 关注会员:3 人
  • 问题浏览:2826
  • 最近回答:2017-06-20
  • X社区推广