有这样一个表
姓名  科目 成绩
张三  语文 80
张三  数学 50
张三  英语 95
张三  物理 75
李四  语文 55
李四  数学 90
李四  英语 65
李四  物理 95
要查询后的结果变为这样排列
姓名 文科成绩 理科成绩 
张三 175       125
李四 120       185
这个SQL语句怎么写。
如果这样排列又应怎么写
姓名  语文 数学 英语 物理
大家帮忙啊

解决方案 »

  1.   

    使用分组 group by
    或者唯一 distinct
      

  2.   

    如果是sql server数据库,请到sql server版搜索关键字"交叉表",结果一大堆
      

  3.   

    第一个:select name as 姓名,(select sum(result) from 
    chengji A where A.Name = B.Name and A.subject in ('语文','英语')) as 文科成绩,
    (select sum(result) from chengji C Where C.name = B.name and C.subject in ('数学','物理')) as 理科成绩
     from chengji B group by name
      

  4.   

    姓名  语文 数学 英语 物理
    这个好像有个povit交叉表语法
      

  5.   

    Select name as 姓名,(Select result From chengji B where B.name = A.name and B.subject='语文') as 语文,(Select result From chengji B where B.name = A.name and B.subject='数学') as 数学,(Select result From chengji B where B.name = A.name and B.subject='英语') as 英语,(Select result From chengji B where B.name = A.name and B.subject='物理') as 物理 from chengji A group by name
      

  6.   

    第二个问题在SQL Server中有一个说法叫转什么来着。反正要declare一个变量。
      

  7.   

    在SQL Server中好象是交叉表,列是固定的,不是动态变化的
      

  8.   

    第一个:select name as 姓名,(select sum(result) from 
    chengji A where A.Name = B.Name and A.subject in ('语文','英语')) as 文科成绩,
    (select sum(result) from chengji C Where C.name = B.name and C.subject in ('数学','物理')) as 理科成绩
     from chengji B group by name这个a.name与b.name a.subject是什么啊
      

  9.   

    第二个问题,在Access中:
    transform sum(成绩) select 姓名 from 表名 group by 姓名 pivot 科目 in ('语文','数学','英语','物理')
      

  10.   

    TO 天剑客,a是表的别名To yxl又学到了一招,谢谢
      

  11.   

    the frist question:
       select name ,(select sum(result) from cj  b where b.name=a.name and b.subject in ('语文','英语'))as 文科成绩,(select sum(result)from cj  c where c.name=a.name and c.subject in ('物理','数学')) as 理科成绩 from cj a groub by a.name
    the secend question:
       select name ,(select result where b.name=a.name b.subject='语文' from cj b) as 语文,(select result where c.name=a.name and c.subject='外语' from cj c),(select result where d.name=a.name and d.subject='数学' from cj d),select result where e.name=a.name and e.subject='物理' from cj e) from cj a group by name