数据库中有如下数据: 
        ID    coreid    term   courseinfo
1265 3347 200801 11111111
1266 3346 200801 11101011
1271 3347 200803 11101001
1272 3346 200803 11101001       现在需要讲coreid值相同的cuorseinfo设置为相同,以term为标准。即结果为:         ID    coreid    term   courseinfo
1265 3347 200801 11111111
1266 3346 200801 11101011
1271 3347 200803 11111111
1272 3346 200803 11101011
由于数据量比较大,手工修改太难,但真不知道sql如何应用循环,希望高手指教,谢谢!

解决方案 »

  1.   

          现在需要讲coreid值相同的cuorseinfo设置为相同,以term为标准。即结果为:         ID    coreid    term  courseinfo 
    1265 3347 200801 11111111 
    1266 3346 200801 11101011 
    courseinfo 有相同?
      

  2.   

    update a set
    courseinfo =(select top 1 courseinfo  from tab where coreid=a.coreid order by term,id)
    from tab a
      

  3.   

    Haiwer ,这种解决角度倒是不错,那么如果我的term列有三种值的情况怎么办呢,若200801,200803,200807,而我采用的是200803的值?
    sql语句中是否没有循环语句的写法呢?谢谢!
      

  4.   

    问题补充,以term为标准的意思就是courseinfo的值,将term为200803值的courseinfo修改为200801的courseinfo值,Haiwer的理解正确。偶表达能力还真是差!
      

  5.   

    update a set
        courseinfo =(select  courseinfo  from tab where coreid=a.coreid and term='200803' )
    from tab a这种写法可以吗,如果term值有多个的话?
      

  6.   


    --1.将记录塞到表TestTB中
    create table TestTB(ID varchar(20),coreid varchar(20),term varchar(20),courseinfo varchar(20))insert into TestTB
    select 1265, 3347, 200801, 11111111 union all
    select 1266, 3346, 200801, 11101011 union all
    select 1271, 3357, 200803, 11101001 union all
    select 1272, 3356, 200803, 12101001 union all
    select 1285, 3347, 200802, 21111111 union all
    select 1286, 3356, 200802, 21101011 union all
    select 1281, 3347, 200803, 21101001 union all
    select 1282, 3346, 200803, 22101001 select * from TestTB 结果:
    ID                   coreid               term                 courseinfo
    -------------------- -------------------- -------------------- --------------------
    1265                 3347                 200801               11111111            
    1266                 3346                 200801               11101011            
    1271                 3357                 200803               11101001            
    1272                 3356                 200803               12101001            
    1285                 3347                 200802               21111111            
    1286                 3356                 200802               21101011            
    1281                 3347                 200803               21101001            
    1282                 3346                 200803               22101001  
    --2.将记录加上编号(按Term,ID排序)塞到临时表#Temp
    --注意:必须用SQL 2005才能行,SQL 2000不支持ROW_NUMBER()函数
    select ROW_NUMBER() OVER (ORDER BY term,id) AS 'RowNumber',* 
    into #Temp from TestTBselect * from #Temp
    --3.将#Temp表中以term为标准的第一条Courseinfo找出来塞至临时表#TempToUpdate中
    select * 
    into #TempToUpdate
    from #Temp a where rowNumber 
    in(select top 1 rowNumber from #Temp where coreid=a.coreid order by rowNumber)select * from #TempToUpdateRowNumber            ID          coreid               term                 courseinfo
    ------------ ---------------- ------------ -------------------- --------------------
    1                    1265        3347                 200801               11111111            
    2                    1266        3346                 200801               11101011            
    4                    1286        3356                 200802               21101011            
    5                    1271        3357                 200803               11101001            
    --4.更新表TestTB
    update TestTB set courseinfo=b.courseinfo from TestTB a,#TempToUpdate b
    where a.coreid=b.coreidselect * from TestTBID                   coreid               term                 courseinfo
    -------------------- -------------------- -------------------- --------------------
    1265                 3347                 200801               11111111            
    1266                 3346                 200801               11101011            
    1271                 3357                 200803               11101001            
    1272                 3356                 200803               21101011            
    1285                 3347                 200802               11111111            
    1286                 3356                 200802               21101011            
    1281                 3347                 200803               11111111            
    1282                 3346                 200803               11101011            (8 行受影响)
      

  7.   


    sql2005with cte as (
    select id,coreid,term,courseinfo,rowid=row_number() over(partition by coreid order by term)
    from #T
    )
    update #T set courseinfo = b.courseinfo
    from #T a
    inner join (select * from cte where rowid=1) b on a.coreid=b.coreid
      

  8.   

    实践与理论真是有差距啊,Haiwer 的方法看起来很正确的,但我不知我中间出了什么差错,数据修改结果错误,现在忙着恢复数据。fmtmbs的值描述非常清楚,有详细过程,方法也异曲同工,我还得继续试试,不过应该解决方法应该就是类似的了,我先恢复一下数据,再整理一下,如果还有其它方法,敬请指教。先做个总结,下午结题吧。
      

  9.   

    结题吧,haiwer的错误原因可能因为update的时候没有定制条件,所以修改时我将所有的courseinfo全部改成相同值了,fmtmbs的详细过程我没有尝试,不过看过程以及测试应该没有什么问题。最后一位朋友的问题应该和haiwer一样,没有where条件。
    不知道我想的对不对,继续思考ing……sql我总是似懂非懂的,看来还得继续学习,谢谢各位了。