怎样通过SQL语句变成 下面的效果:        .net  java  SQL张三    80      85   95李四    86      92王五    96     88    78

解决方案 »

  1.   

      
     上面图片未显示  ,这是图片内容:  name    sub    score
      张三     .net    80  张三      java    85  张三      SQL     95  李四      .net    86  李四      java    92
         
      王五      .net    96  王五      java    88  王五      SQL     78
      

  2.   


    create table dan
    (name varchar(6), sub varchar(6), score int)insert into dan
    select '张三', '.net', 80 union all
    select '张三', 'java', 85 union all
    select '张三', 'SQL', 95 union all
    select '李四', '.net', 86 union all
    select '李四', 'java', 92 union all
    select '王五', '.net', 96 union all
    select '王五', 'java', 88 union all
    select '王五', 'SQL', 78
    select name,
    isnull([.net],'') [.net],
    isnull([java],'') [java],
    isnull([SQL],'') [SQL]
    from dan a
    pivot(max(score) for sub in ([.net],[java],[SQL])) b
    name   .net        java        SQL
    ------ ----------- ----------- -----------
    李四     86          92          0
    王五     96          88          78
    张三     80          85          95(3 row(s) affected)
      

  3.   


    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    create table [test]([name] varchar(4),[sub] varchar(4),[score] int)
    insert [test]
    select '张三','.net',80 union all
    select '张三','java',85 union all
    select '张三','SQL',95 union all
    select '李四','.net',86 union all
    select '李四','java',92 union all
    select '王五','.net',96 union all
    select '王五','java',88 union all
    select '王五','SQL',78select * 
    from 
        [test] 
    pivot 
        (max([Score]) for [sub] in([.net],[java],[SQL]))b
    /*
    name .net java SQL
    李四 86 92 NULL
    王五 96 88 78
    张三 80 85 95
    */
      

  4.   


    create table tbc 
    (name VARCHAR(100), sub VARCHAR(100), score INT)insert into tbc
    SELECT '张三', '.net', 80 UNIONSELECT '张三', 'java', 85 UNIONSELECT '张三', 'SQL', 95 UNIONSELECT '李四', '.net', 86 UNIONSELECT '李四', 'java', 92 UNION
       
    SELECT '王五', '.net', 96 UNIONSELECT '王五', 'java', 88 UNIONSELECT '王五', 'SQL', 78
    select name,[.net],[java],[sql]
    from tbc
    pivot (max(score) for sub in([.net],[java],sql)) as d 
    name .net java sql
    李四 86 92 NULL
    王五 96 88 78
    张三 80 85 95