表A如下:
Code Name Scores
1     A     100
1     B     90
1     C     85
1     D     60
2     A     92
2     C     72
3     B     83现在需要将Name是ABC的数据抽出来,并且显示为如下
注:S1 是 Name 为A 的分数,S2是B的分数,以此类推Code  S1    S2   S3
1     100   90   85
2     92         72
3           83根据“能抓到老鼠的猫就是好猫”的原则,方法不限,任何方法均可,只要能得到显示结果既可

解决方案 »

  1.   

    select code,
    case Name when 'A' then Scores end as S1,
    case Name when 'B' then Scores end as S2,
    case Name when 'C' then Scores end as S3
    from 表A 
    group by code
      

  2.   

    select a.code, 
      (select top 1 score from score where code = a.code and name = 'A')  as S1,
      (select top 1 score from score where code = a.code and name = 'B')  as S2,
      (select top 1 score from score where code = a.code and name = 'C')  as S3,
    from score a
      

  3.   

    SELECT DISTINCT Code,
              (SELECT TOP 1 scores
             FROM tmp
             WHERE code = a.code AND name = 'A') AS S1,
              (SELECT TOP 1 scores
             FROM tmp
             WHERE code = a.code AND name = 'B') AS S2,
              (SELECT TOP 1 scores
             FROM tmp
             WHERE code = a.code AND name = 'C') AS S3
    FROM tmp a
      

  4.   

    select * from (select score s1,'' as s2,'' as s3 from A where name ='a'
    union
    select '' as s1,score as s2,'' as s3 from A where name='b'
    union
    select '' as s1,'' as s2,score as s3 from A where name='c') aaa oreder by score
      

  5.   

    mxldream(小石头) ( ) 信誉:100  的方法跟Paradox85(PP) ( ) 信誉:100  方法一样,得出的结果都是
    code为1的Code  S1    S2   S3
    1     100   90   85
    2     100   90   85
    3     100   90   85
      

  6.   

    select code,
    S1 =sum(case Name when 'A' then  (Score)  end)  ,
    S2 =sum(case Name when 'B' then  (Score) end) ,
    s3 =sum(case Name when 'C' then  (Score) end) 
    from TABLE1 
    group by code
    SELECT * FROM TABLE1
      

  7.   

    select code,
    sum(case name when 'A' then score else null end) S1 ,
    sum(case name when 'B' then score else null end) S2 ,
    sum(case name when 'C' then score else null end) S3
    from ABC
    group by code
      

  8.   

    SELECT DISTINCT Code,
              (SELECT TOP 1 scores
             FROM test
             WHERE code = a.code AND name = 'A') AS S1,
              (SELECT TOP 1 scores
             FROM test
             WHERE code = a.code AND name = 'B') AS S2,
              (SELECT TOP 1 scores
             FROM test
             WHERE code = a.code AND name = 'C') AS S3
    FROM test a
      

  9.   

    无ABC限制
    DECLARE @s VARCHAR(4000)
    SET @s='Code'
    SELECT @s=@s+',['+Name+']=sum(case Name when '''+Name+''' then score else 0 end)'
    from table1 group by  Codeset @s='select '+@s+' from table1 group by Code
    exec (@s)
      

  10.   

    我晕哦,一看代码格式就知道,我和 jhhujia(兔子) 的代码都在SQL中执行,测试过的,怎么会不对呢?!
      

  11.   

    是不是Name里的值是不定的?不一定是A,B,C?而是有多少就有多少列?
      

  12.   

    那错怪Paradox85(PP) ( ) 信誉:100 和兔子兄了,对不起啊
    不过我的运行结果就是这样的Code  S1    S2   S3
    1     100   90   85
    2     100   90   85
    3     100   90   85Name列不一定有多少,但是,最多选3个就够了,呵呵~~
      

  13.   

    select b.* ,
    S1 = (Select  scores from 表名 where code=b.code and name='A'),
    S1 = (Select  scores from 表名 where code=b.code and name='B'),
    S1 = (Select  scores from 表名 where code=b.code and name='C')
    from(select  DISTINCT code 
    from 表名)b
    试试这个
      

  14.   

    LZ,求你了,给点面子我,呜呜呜,测一下
    select code,
    S1 =sum(case Name when 'A' then  (Score)  end)  ,
    S2 =sum(case Name when 'B' then  (Score) end) ,
    s3 =sum(case Name when 'C' then  (Score) end) 
    from TABLE1 
    group by code
    SELECT * FROM TABLE1
      

  15.   

    zhongkeruanjian(编程亮子) ( ) 信誉:75 
    好兄弟,你的可以,我试过了,
    不过存在一个小小的问题,我题目的是scores,你写的是score,看题不认真,
    扣1分,哈哈,不过肯定不会给你1分,放心好了。
    别人也都有份
      

  16.   

    mbh0210(独孤求败) ( ) 信誉:100 
    的方法也不错
      

  17.   

    我想问一下你的列S1,S2,S3是否就只是有三列,我想问的是看是否是下面有多少个NAME就显示出多少列来.如果需求只有三列求败的应该能实现的.如果要是可变列的话我到是可以帮你想一想.
      

  18.   

    友情提示:score是不可数名词。。-_-**
      

  19.   

    xr105(飞花逐月) : nAME 无限制DECLARE @s VARCHAR(4000)
    SET @s='Code'
    SELECT @s=@s+',['+Name+']=sum(case Name when '''+Name+''' then score else 0 end)'
    from table1 group by  Codeset @s='select '+@s+' from table1 group by Code
    exec (@s)
      

  20.   

    ----初始化环境------------------
    create table grade(
    Code  int not null,
    Name  varchar(10),
    Scores int)insert into grade 
    select 1,     'A',     100 
    union select 1,     'B',     90
    union select 1,     'C',     85
    union select 1,     'D',     60
    union select 2,     'A',     92
    union select 2,     'C',     72
    union select 3,     'B',     83
    ----查询---------------------------
    select * from grade----测试---------------------------declare @sql varchar(8000)
    set @sql = 'select code '
    select @sql = @sql + ', sum(case [Name] when ''' + [Name] + ''' then scores else null end) as [s'+ [name] + ']'
    from grade group by Name
    select @sql = @sql + ' from grade group by code'
    print @sql
    exec (@sql)----清除---------------------------
    drop table grade----结果--------------------------
    code     sA       sB        sC       sD
    1 100 90 85 60
    2 92 NULL 72 NULL
    3 NULL 83 NULL NULL
      

  21.   

    十分感谢大家的热心帮忙,现在还有一个小问题,
    还是选择Name是ABC的
    但是要将分数都统计出来,又该怎么写呢? 
      

  22.   

    SELECT @s=@s+',['+Name+']=sum(case Name when '''+Name+''' then score else 0 end)'
    from table1 group by  Code这种方法太强了.我是头一次看到,刚研究一下,看明白了,不知道下次是否记得用.呵.
      

  23.   

    呵呵,meiqingsong(阿飛) 的方法也不错啊
    这里强人云集啊
      

  24.   

    select distinct code,
    (select score from table1 where code =a.code and name ='A') as s1,
    (select score from table1 where code =a.code and name ='A') as s2,
    (select score from table1 where code =a.code and name ='A') as s3
    from table1 a还可以用执行字符串的方式不限制列数,也就是A,B,C不限制
    可以和我交流QQ43950411
      

  25.   

    上面有点错误: name ='b',name = 'c'