字段A(VARCHAR)   字段B(INT)   字段C(INT)     字段D(DATETMIE)     字段E(INT)
    A                2004        1              2004-6-10 
    A                2004        0              2004-6-11 
    A                2005        3              2004-6-12 
    A                2004        1              2004-6-13
    A                2004        1              2004-6-14 
    A                2004        0              2004-6-15 
    A                2004        3              2004-6-16 
    b                2004        1              2004-6-17 
    b                2004        0              2004-6-18 
    b                2004        0              2004-6-19 
    b                2005        0              2004-6-20 
    b                2004        3              2004-6-21 
    b                2004        1              2004-6-22 
    b                2004        3              2004-6-23 
    b                2004        1              2004-6-24 
    b                2004        1              2004-6-25想写一个UPDATE  更新字段E   结果是   =字段A和=字段B  时间小于当前更新行的时间  的前五行数据行中  字段C >0 的数量    如不足五行则返回null

解决方案 »

  1.   

    如第一行   小于他的时间的行不足五行则结果为NULL
      第二行   小于他的时间的行不足五行则结果为NULL
    第三行   小于他的时间的行不足五行则结果为NULL
      第四行   小于他的时间的行不足五行则结果为NULL
    第五行   小于他的时间的行不足五行则结果为NULL
      第六行   小于他的时间的五行中 c>0 的数量为4  结果为4
    第七行    小于他的时间的五行中  c>0的数量也是为4  结果为4
    第八行    小于他的时间的行不足五行则结果为NULL
    .....以此类推
      

  2.   

    --测试数据
    create table t1(a varchar,b int,c int,d datetime,e int)
    insert into t1(a,b,c,d)
    select 'A',2004,1, '2004-6-10'
    union all select 'A',2004,0,'2004-6-11'
    union all select 'A', 2005, 3,'2004-6-12' 
    union all select 'A', 2004, 1,'2004-6-13'
    union all select 'A', 2004, 1,'2004-6-14' 
    union all select 'A', 2004, 0,'2004-6-15' 
    union all select 'A', 2004, 3,'2004-6-16' 
    union all select 'b', 2004, 1,'2004-6-17' 
    union all select 'b', 2004, 0,'2004-6-18' 
    union all select 'b', 2004, 0,'2004-6-19' 
    union all select 'b', 2005, 0,'2004-6-20' 
    union all select 'b', 2004, 3,'2004-6-21' 
    union all select 'b', 2004, 1,'2004-6-22' 
    union all select 'b', 2004, 3,'2004-6-23' 
    union all select 'b', 2004, 1,'2004-6-24' 
    union all select 'b', 2004, 1,'2004-6-25'update t1
    set e = case when (select count(*) from t1 a where a.d < t1.d) >= 5 then (select count(*) from t1 a where a.c > 0 and a.d < t1.d) end
    from t1--输出结果
    select * from t1
      

  3.   

    ----创建测试数据
    declare @t table(id int,A varchar(10),B int,C int ,D datetime,E int)
    insert @t
    select 1,'A',2004,1,'2004-6-10',0 union all
    select 2,'A',2004,0,'2004-6-11',0 union all
    select 3,'A',2005,3,'2004-6-12',0 union all
    select 4,'A',2004,1,'2004-6-13',0 union all
    select 5,'A',2004,1,'2004-6-14',0 union all
    select 6,'A',2004,0,'2004-6-15',0 union all
    select 7,'A',2004,3,'2004-6-16',0 union all
    select 8,'B',2004,1,'2004-6-17',0 union all
    select 9,'B',2004,0,'2004-6-18',0 union all
    select 10,'B',2004,0,'2004-6-19',0 union all
    select 11,'B',2005,0,'2004-6-20',0 union all
    select 12,'B',2004,3,'2004-6-21',0 union all
    select 13,'B',2004,1,'2004-6-22',0 union all
    select 14,'B',2004,3,'2004-6-23',0 union all
    select 15,'B',2004,1,'2004-6-24',0 union all
    select 16,'B',2004,1,'2004-6-25',0
    ----更新
    UPDATE x SET E = y.ct 
    FROM @t x left join 
    (SELECT id,ct = (select count(*) from @t where A = a.A and D< a.D and C>0) from @t a 
    where (select count(*) from @t where A = a.A and D < a.D and C> 0) >=4) y on x.id = y.id
    ----查看更新结果
    select * from @t