zoukankan      html  css  js  c++  java
  • 利用sql的OVER()PARTITION 找到最相近的数值

    前几天同事问我一个问题,能不能用sql搞定这个问题:

    我这里有一个张表table1中有time1,value1,有表table2有字段time2,value2。

    现在要把table2中的value2更新到table1的value1中,要求time2距离time1最近。

    一下子就想到之前学到的over partititon,但是只是学的时候操作了几个例子,一下子也没写上来。

    后来找到一个教程http://blog.csdn.net/ayou2008/article/details/7179001

      1. select e.ename,  
      2.        e.job,  
      3.        e.sal,  
      4.        e.deptno,  
      5.        lead(e.sal, 1, 0) over(partition by e.deptno order by e.sal) lead_sal,  
      6.        lag(e.sal, 1, 0) over(partition by e.deptno order by e.sal) lag_sal,  
      7.        nvl(lead(e.sal) over(partition by e.deptno order by e.sal) - e.sal,  
      8.            0) diff_lead_sal,  
      9.        nvl(e.sal - lag(e.sal) over(partition by e.deptno order by e.sal), 0) diff_lag_sal  
      10.   from scott.emp e;  

    稍微修改一下即可,diff_lead_sal, diff_lag_sal  sal做差  两个差中找比较小的一个

    请诸位试试能不能做到。

    但是感觉效率很差,大家有没有好的方法啊?那哥们要离职了,根本不考虑太多,拿起我的方案就用...

  • 相关阅读:
    刘汝佳,竖式问题
    二叉树
    sort
    hash
    贪心算法
    FFT,NTT 专题
    hdu 5861 Road 两棵线段树
    hdu 5862 Counting Intersections
    hdu 5833 Zhu and 772002 ccpc网络赛 高斯消元法
    hdu 5800 To My Girlfriend + dp
  • 原文地址:https://www.cnblogs.com/13579net/p/3141398.html
Copyright © 2011-2022 走看看