数据表:ID   测试者  测试项目     上次值    本次值
1     1         1            92       100
2     1         2            82       90
4     2         1            88       99
5     2         2            86       95
.................................请问如何得到下面这样的结果
测试者   测试项目1上次值  测试项目1本次值  测试项目2上次值 测试项目2本次值
1             92            100                 80            90
2             88             99                 86            95还有行转列以后怎样更新到数据库?
请各位不吝指教,小弟感激不尽~~~~~~~!

解决方案 »

  1.   

    create table test(ID int,测试者 int,测试项目 int,上次值 int,本次值 int)
    insert test select 1,1,1,92,100
    union all select 2,1,2,82,90
    union all select 4,2,1,88,99
    union all select 5,2,2,86,95
    --drop table testdeclare @s varchar(8000)
    set @s='select 测试者'
    select @s=@s+',max(case bh when '''+rtrim(bh)+''' then 上次值 else 0 end) as 测试项目'+rtrim(bh)+'上次值
    ,max(case bh when '''+rtrim(bh)+''' then 本次值 else 0 end) as 测试项目'+rtrim(bh)+'本次值'
    from (select *,bh=(select count(1) from test where 测试者=b1.测试者 and ID<=b1.ID) from test b1)t 
    group by bhselect @s=@s+' from (select *,bh=(select count(1) from test where 测试者=b1.测试者 and ID<=b1.ID) from test b1)t
     group by 测试者'exec(@s)drop table test测试者    测试项目1上次值 测试项目1本次值 测试项目2上次值 测试项目2本次值    
    ----------- ----------- ----------- ----------- ----------- 
    1           92          100         82          90
    2           88          99          86          95
      

  2.   

    非常感谢bill024(捞猴子的月亮) 
    行转列以后怎样更新到数据库呢?
    比如 我在前台修改了测试项目1本次值,怎样更新到数据库呢?
      

  3.   

    create table table1
    (
    ID   int identity(1,1),user_id  int,xm_id      int,  value1  int,  value2 int)insert into table1
    select 1,1,92,100 union all
    select 1,2,22,90 union all
    select 2,1,88,99 union all
    select 2,2,88,99
    select user_id,
    max(
    case xm_id
        when 1 then value1
    end) as 測試項目1上次值,
    max(
    case when xm_id=1 then value2
    end) as 測試項目1本次值,
    max(
    case xm_id
        when 2 then value1
    end) as 測試項目2上次值,
    max(
    case when xm_id=2 then value2
    end) as 測試項目2本次值
    from table1
    group by user_id
      

  4.   

    bykai() 你到底明不明白我的意思
    我是说行转列后在程序前台显示,如果修改数据点击保存后怎样更新?根据哪些条件更新?
      

  5.   

    知道修改数据库的主键,直接用SQL语句更新吧
      

  6.   

    happyflystone(无枪的狙击手) 
    能说说怎样直接更新吗?问题就是怎样知道修改的是哪条数据,得到主键就可以了