select 学生,case when 书名='小说' then 数量 end,
case when 书名='散文' then 数量 end,case when 书名='其他' then 数量 end,

解决方案 »

  1.   

    阁下,我实际表的学生项的数量有几千个,书名也有几千个,而且是动态变化的,如果这样的SQL语言会很长,请问还有其他方法吗,请赐教!
      

  2.   

    select 学生,sum(case when 书名='小说' then 数量 end),
    sum(case when 书名='散文' then 数量 end),sum(case when 书名='其他' then 1 end) from .... group by 学生
      

  3.   

    刚学的:
    select 学生 ,sum(case 书名 when '小说' then 数量 else 0 end) as '小说' , sum(case 书名 when '散文' then 数量 else 0 end) as '散文' , sum(case 书名 when '其他' then 数量 else 0 end) as '其他' from table group by 学生
      

  4.   


    dim q as string,ds as ADODB.Recordset
    ds.open "select distinct 书名 as a  from ...".....
    do until ds.eof
        if q<>"" then Q=Q & ","
        q=q & sum(case when 书名='" & ds("a") &"' then 数量 end),
        ds.movenext
    loop
    ds.close
    ds.open "select 学生, " & Q & "from .... group by 学生"...
      

  5.   

    可以用java 写吗,我是把SQL嵌入java 的
      

  6.   

    没有必要一定用SQL;
    可以把第一个表读出来,然后程序中作个判断,再显示出来。