本帖最后由 maoweiting19910402 于 2012-04-13 15:17:08 编辑

解决方案 »

  1.   


    SQL> select * from A
      2  /
     
            ID        AGE   LAST_AGE         OK
    ---------- ---------- ---------- ----------
             1         15                     1
             2         19                     0
             3         12                     1
             4         18                     1
             5         35                     1
             6         23                     1
             7         16                     1
             8         24                     1
             9         13                     1
            10          3                     1
     
    10 rows selected
      

  2.   

    要得出的数据     ID        AGE   LAST_AGE         OK
    ---------- ---------- ---------- ----------
             1         15      14             1
             2         19      18             0
             3         12      3              1
             4         18      16             1
             5         35      24             1
             6         23      18             1
             7         16      15             1
             8         24      23             1
             9         13      12             1
            10          3                     1   
      

  3.   


    update a set last_age=
    (select b.age from a b where a.id!=b.id and ok is not null and ok !=0 and a.age>b.age
    rownum=1 order by a.age-b.age )
      

  4.   

    亲,这个rownum好像是先=1再排序
      

  5.   


    create table A(ID number not null,age number,last_age number,OK number);insert into a values (1,15,null,1);
    insert into a values (2,19,null,0);
    insert into a values (3,12,null,1);
    insert into a values (4,18,null,1);
    insert into a values (5,35,null,1);
    insert into a values (6,23,null,1);
    insert into a values (7,16,null,1);
    insert into a values (8,24,null,1);
    insert into a values (9,13,null,1);
    insert into a values (10,3,null,1);
    insert into a values (11,15,null,1);
    insert into a values (12,23,null,1);select id,age,(select distinct max(b.age) from a b where b.age < a.age and b.ok is not null and b.ok <> 0) last_age,ok
    from a    id    age    last_age   ok
    -----------------------------------------
    1 1 15 13 1
    2 2 19 18 0
    3 3 12 3 1
    4 4 18 16 1
    5 5 35 24 1
    6 6 23 18 1
    7 7 16 15 1
    8 8 24 23 1
    9 9 13 12 1
    10 10 3 1
    11 11 15 13 1
    12 12 23 18 1
      

  6.   

    测试数据:CREATE TABLE T184
    (
        ID NUMBER NOT NULL,
        age NUMBER,
        last_age NUMBER,
        OK NUMBER
    );INSERT INTO T184 VALUES(1, 15, NULL, 1);
    INSERT INTO T184 VALUES(2, 19, NULL, 0);
    INSERT INTO T184 VALUES(3, 12, NULL, 1);
    INSERT INTO T184 VALUES(4, 18, NULL, 1);
    INSERT INTO T184 VALUES(5, 35, NULL, 1);
    INSERT INTO T184 VALUES(6, 23, NULL, 1);
    INSERT INTO T184 VALUES(7, 16, NULL, 1);
    INSERT INTO T184 VALUES(8, 24, NULL, 1);
    INSERT INTO T184 VALUES(9, 13, NULL, 1);
    INSERT INTO T184 VALUES(10, 3, NULL, 1);
    测试结果:
      

  7.   

    使用rollup()比较前一和后一吧!
      

  8.   

    测试数据:CREATE TABLE T184
    (
        ID NUMBER NOT NULL,
        age NUMBER,
        last_age NUMBER,
        OK NUMBER
    );INSERT INTO T184 VALUES(1, 15, NULL, 1);
    INSERT INTO T184 VALUES(2, 19, NULL, 0);
    INSERT INTO T184 VALUES(3, 12, NULL, 1);
    INSERT INTO T184 VALUES(4, 18, NULL, 1);
    INSERT INTO T184 VALUES(5, 35, NULL, 1);
    INSERT INTO T184 VALUES(6, 23, NULL, 1);
    INSERT INTO T184 VALUES(7, 16, NULL, 1);
    INSERT INTO T184 VALUES(8, 24, NULL, 1);
    INSERT INTO T184 VALUES(9, 13, NULL, 1);
    INSERT INTO T184 VALUES(10, 3, NULL, 1);
    测试结果:
      

  9.   

    create table B
    (
    name number,
    what   varchar(20),
    place  varchar(20),
    tim    date,
    money  number,
    last   number,
    primary key(name,place,tim)
    )SQL> select * from B;
     
          NAME WHAT                 PLACE                TIM              MONEY   Last
    ---------- -------------------- -------------------- ----------- ----------  ----------
             1 1                    超市                 2012/4/13           12
             1 1                    超市                 2012/4/11           13
             1 0                    超市                 2012/4/10           14
             2 1                    学校                 2012/4/7            16
             2 1                    学校                 2012/4/16           31
             2 1                    学校                 2012/4/26           12
     
    6 rows selected
    不好意思题目模拟描述有问题是某人去某地买东西last=前面一个日期在同一个地方买的东西的钱,不为0不为空的数据,what=0的数据不算在内
      

  10.   


    create table B
    (
    name number,
    what   varchar(20),
    place  varchar(20),
    tim    date,
    money  number,
    last   number,
    primary key(name,place,tim) 
    )
    insert into b values (1,1,'超市',date'2012-04-13',12,null);
    insert into b values (1,1,'超市',date'2012-04-11',13,null);
    insert into b values (1,0,'超市',date'2012-04-10',14,null);
    insert into b values (2,1,'学校',date'2012-04-07',16,null);
    insert into b values (2,1,'学校',date'2012-04-16',31,null);
    insert into b values (2,1,'学校',date'2012-04-26',12,null);select name,what,place,tim,money,
           (select max(a.money) from b a where a.tim < b.tim and a.what is not null and a.what <> 0 and b.name=a.name) last
    from b    name   what    place    tim        money   last
    -----------------------------------------------------------
    1 1 1 超市 2012/4/13 12 13
    2 1 1 超市 2012/4/11 13
    3 1 0 超市 2012/4/10 14
    4 2 1 学校 2012/4/7 16
    5 2 1 学校 2012/4/16 31 16
    6 2 1 学校 2012/4/26 12 31
      

  11.   

    一般在执行update前都会先查询下更改的结果是否正确  
    update b set last=(select max(a.money) from b a where a.tim < b.tim and a.what is not null and a.what <> 0 and b.name=a.name) ;select * from b;    name  what place   tim       money  last
    -----------------------------------------------------------
    1    1    1    超市    2012/4/13    12    13
    2    1    1    超市    2012/4/11    13    
    3    1    0    超市    2012/4/10    14    
    4    2    1    学校    2012/4/7     16    
    5    2    1    学校    2012/4/16    31    16
    6    2    1    学校    2012/4/26    12    31
      

  12.   

    好像这样也可以
    select t.*,Lead(money,1) over( partition by name order by tim desc) from luojf_b T
    where t.what is not null and t.what <> 0
      

  13.   

          NAME WHAT                 PLACE                TIM              MONEY       LAST LEAD(MONEY,1)OVER(PARTITIONBYN
    ---------- -------------------- -------------------- ----------- ---------- ---------- ------------------------------
             1 1                    超市                 2012-4-13           12                                        13
             1 1                    超市                 2012-4-11           13            
             2 1                    学校                 2012-4-26           12                                        31
             2 1                    学校                 2012-4-16           31                                        16
             2 1                    学校                 2012-4-7            16            
     
      

  14.   


    update b
       set b.last =
           (select money1
              from (select name,
                            place,
                            tim,
                            lag(money, 1)over(partition by name, place order by tim) money1 
                      from b)  c
             where b.name = c.name
               and b.place = c.place
               and b.tim = c.tim);
      

  15.   

    我也是这么做的,导师点评说
    1.按照这样的做法 what=0的前面一个或者money is null或money等于0的这些记录无法找到上一次
    2.表非常大,一次性commit不行
      

  16.   

    update b set last=
    (select max(a.money) from b a where a.tim=(select max(c.tim) from b c where c.tim<b.tim and c.what <> 0)
     and a.what is not null and b.name=a.name and a.place=b.place)
    /
     
      

  17.   

    1、为了更加稳妥,应先把select语句写出来,再进行update
    2、使用lag over,对于age相同的记录,存在问题,需要先去重复。--方案1、一般思路
    select t.sid, t.age, (select max(s.age) from sl_test s where s.age<t.age and nvl(s.ok,0)!=0) last_age
      from sl_test t;
    --方案2、使用lag over,对于ok为0的记录,仍有问题。
      

  18.   


    "what=0的前面一个或者money is null或money等于0的这些记录无法找到上一次"  这个你不是可以在“select money1”上加处理吗,有考虑这个,但是不知道对这种情况你的要求是怎么处理,应该是好解决的“表非常大,一次性commit不行”,不知道你的表有多大,你可以去了解一下在Oracle下将事务分多次commit和与一次commit时的比较和分析。更新耗时和资源消耗的情况得看你具体的数据量,要得到较优的处理得自己做比较,没有一次commit或多次commit的选择哪个一定会更好的标准说法。
      

  19.   

    select money1这个怎么处理  详细说说看 谢谢