动态Sql语句肯定能实现,这几天写的太多了...楼主你随便搜搜就能找到类似的问题

解决方案 »

  1.   

    Select s.name as ‘姓名’
    (select p.points from points p  where p.id=s.sid and p.cid in (select c.cid from course c where c.cname=’语文’) ) as ‘ 语文’,
    (select p.points from points p  where p.id=s.sid and p.cid in (select c.cid from course c where c.cname=’数学’) ) as ‘ 数学’,
    (select p.points from points p  where p.id=s.sid and p.cid in (select c.cid from course c where c.cname=’英语’) ) as ‘ 英语’ 
    from student s where s.class=’001’
    有种是这样做,有那个高手有更好的解法
      

  2.   

    --参考/*-- 数据测试环境 --*/
    if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [tb]
    GOcreate table tb(单位名称 varchar(10),日期 datetime,销售额 int)
    insert into tb
     select 'A单位','2001-01-01',100
     union all select 'B单位','2001-01-02',101
     union all select 'C单位','2001-01-03',102
     union all select 'D单位','2001-01-04',103
     union all select 'E单位','2001-01-05',104
     union all select 'F单位','2001-01-06',105
     union all select 'G单位','2001-01-07',106
     union all select 'H单位','2001-01-08',107
     union all select 'I单位','2001-01-09',108
     union all select 'J单位','2001-01-11',109/*-- 要求结果
    日期       A单位  B单位 C单位 D单位 E单位  F单位 G单位 H单位 I单位 J单位   
    ---------- ----- ----- ----- ----- ----- ----- ----  ----  ---- ------
    2001-01-01 100   0     0     0     0     0     0     0     0     0
    2001-01-02 0     101   0     0     0     0     0     0     0     0
    2001-01-03 0     0     102   0     0     0     0     0     0     0
    2001-01-04 0     0     0     103   0     0     0     0     0     0
    2001-01-05 0     0     0     0     104   0     0     0     0     0
    2001-01-06 0     0     0     0     0     105   0     0     0     0
    2001-01-07 0     0     0     0     0     0     106   0     0     0
    2001-01-08 0     0     0     0     0     0     0     107   0     0
    2001-01-09 0     0     0     0     0     0     0     0     108   0
    2001-01-11 0     0     0     0     0     0     0     0     0     109
    --*//*-- 常规处理方法*/ 
    declare @sql varchar(8000)
    set @sql='select 日期=convert(varchar(10),日期,120)'
    select @sql=@sql+',['+单位名称
     +']=sum(case 单位名称 when '''+单位名称+''' then 销售额 else 0 end)'
    from(select distinct 单位名称 from tb) a
    exec(@sql+' from tb group by convert(varchar(10),日期,120)')
      

  3.   

    先汇总再转为横向,这几天写的太多了,你可看看这个帖子,看我最后一个回复:
    http://community.csdn.net/Expert/topic/5529/5529672.xml?temp=.6944544
      

  4.   

    能用一个SQL写出来确实有点难度
      

  5.   

    select * from 
      xs,kc,fs 
    where 
       xs.class = kc.class 
      and 
       kc.cid=fs.cid  
      and 
       xs.sid=fs.sid  
      and 
       xs.class=1随便写的不知道是否正确。
      

  6.   

    select 学生表.姓名 as 姓名,分数,分数表.分数 as 分数,课程表.课程名称 as 课程名称 from 分数表,学生表,课程表 where 学生表.学号=分数表.学号 and 分数表.课程号=课程表.课程号  and 学生表.班级='1'
      

  7.   

    我指的是事先不知道有多少科,要动态查询的话,用一个SQL写出来有难度, 如果事先知道有多少科, 那就是小儿科的事了