请教大家一个问题,下面这样一个表:
material         assy#                                                 Build Date wipdemand
11-1731-01$1         73-10098-18                                        2011-08-24 432.000
11-1731-01$1         73-10098-18                                        2011-08-20 432.000
11-1731-01$1         73-12886-04                                        2011-08-26 400.000
11-1731-01$1         73-12886-04                                        2011-08-17 400.000
11-2286-01$1         73-10098-18                                        2011-08-20 378.000
11-2286-01$1         73-10098-18                                        2011-08-24 378.000
11-2286-01$1         73-10899-16C                                       2011-08-18 350.000
11-2286-01$1         73-10899-17                                        2011-08-28 70.000一个条件,在相同material,相同assy#前提下,该条件下的第一行不变,其它行更新wipdemand为0.sql 如何写?

解决方案 »

  1.   

    首先确定“第一行”怎么定义的,Created date时间最早吗?
    如果这样,只要更新 material, assy# 相同, 并且Created Date > min(Created Date)就可以了。
      

  2.   

    源表是有主键的,我这里列出的是其中几列。
    Created Date > min(Created Date) 这个条件不行啊,我这个表是SELECT 出来的。CreateDate都是同一个时间生成的。
      

  3.   

    用LINQ to SQL 试试
    假设表名称为Table1 ,db为datacontact 的一个实例var query=(from u in db.table
               group u by u.material into g
               select new
               {
                   g.key,
                   Count=(from n in g
                         group n by n.iassy# into m
                         select m).take(1)
               }foreach(var v in query)
    {
          console.WriteLine(v.key)                    
          console.WriteLine(v.g.material+" "+v.g.assy# Build+" "+v.g.Build_Date+" "+v.g.wipdemand
    )
    }
      

  4.   

    update tb
    set wipdemand=0
    from tb a
    where exists(select 1 
    from tb b 
    where b.material=a.material 
    and b.assy#=a.assy#
    and b.wipdemand=a.wipdemand
    and b.BuildDate>a.BuildDate)
    tb是表名,默认第一条是日期最小的一条,且日期不会重复。
      

  5.   

    --假设表存在主键自增idupdate t
    set wipdemand = 0
    from tb t
    where id not in (select min(id) from tb group by material,assy#)
      

  6.   


    and b.BuildDate>a.BuildDate)这里应该是and b.BuildDate<a.BuildDate才对,刚才搞错了
      

  7.   

    update
     tb
    set
     wipdemand=0
    from
     tb t
    where
     BuildDate=(select max(b.BuildDate) from tb where material=t.material and b.assy#=a.assy# and b.wipdemand=a.wipdemand )
      

  8.   

    请问楼上的,where这里要这么写的话,只UPDATE最大值了?怎么样能实现第一行不变,其它行的在相同material,相同assy#前提下,更新WIPDEMAND为0呢?、
     BuildDate=(select max(b.BuildDate) from tb where material=t.material and b.assy#=a.assy# and b.wipdemand=a.wipdemand )
      

  9.   

    结贴了。BuildDate=(select max(b.BuildDate) from tb where material=t.material and b.assy#=a.assy# and b.wipdemand=a.wipdemand )