teradata到mysql查询转换问题

qqrboqgw  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(340)

teradata公司query:-

select  min(elapsed_sec) over (partition by job_name, parent_job_name order by elapsed_sec rows between 1 following and 1 following) - elapsed_sec as diff from xyz

teradata输出query:-

elapsed_sec row_count row_no diff
0.000       207        143  1.000
1.000       207        144  0.000
1.000       207        145  0.000
1.000       207        146  0.000
1.000       207        147  4,555.000
4,556.000   207        148  1,250.000
5,806.000   207        149  2,038.000
7,844.000   207        150  73.000

note:- diff 是当前行和下一行之间的差值
例如,1-0=1在第一行,1-1=0在第二行我的mysql5.6不支持这个功能。请帮忙得到想要的结果

ss2ws0br

ss2ws0br1#

您的代码可以在mysql的最新版本中工作。所以,我的第一个建议是更新到mysql 8+。
在此之前,一种方法是关联子查询:

select ( (select min(elapsed_sec) 
          from xyz xyz2
          where xyz2.job_name = xyz.job_name and
                xyz2.parent_job_name = xyz.parent_job_name and
                xyz2.elapsed_sec > xyz.elapsed_sec
         ) - elapsed_sec) as diff
from xyz;

相关问题