姓名   语文   数学    英语  化学
   张三    88    98      100    97
   王五    89    79      98     95
   李四    78    88      95     86 
   王开   85     96      85     95  
上表为数据库的表现在我要查询出来在界面上显示如下:
   姓名   语文   数学    英语  化学
   张三    88     98      100    97
    王五   89     79      98     95
    李四   78     88      95     86 
    王开   85     96      85     95  
    合计   340    361     378    373这个SQL语句怎么写啊,高手帮忙啊!!!(就是要把合计算出来,用一个SQL语句查询出来)  在 SQL Server  2000 中

解决方案 »

  1.   

    --try
    select
    case when grouping(姓名)=1
    then '合计' else 姓名 end as 姓名,
    sum(语文) as 语文,
    sum(数学) as 数学,
    sum(英语) as 英语,
    sum(化学) as 化学
    from tablename
    group by 姓名 with rollup
      

  2.   

    DECLARE @t TABLE([姓名] varchar(20), [语文] varchar(20), [数学] varchar(20), [英语] varchar(20), [化学] varchar(20))
    INSERT @t SELECT * from <table> UNION ALL 
    SELECT '合计' as 姓名, sum(语文) as 语文, sum(数学) as 数学, sum(英语) as 英语,  sum(化学) as 化学
      

  3.   

    --分类查询查询描述
    --rollup操作符:选列中值的某一层次结构的聚合,返回单个结果集
    --cube操作符:所选列中所有组合的聚合,返回单个结果集
    --gouping操作符:判断结果集中的值是本来就有的,还是使用了rollup或cube以后产生的
    --compute子句:不支持text、ntext、image数据类型,子句里的字段要和select中的字段相同,为字段创建详细的
    --             记录和汇总值,返回多个结果集
    --compute by子句:需要和order by一起使用,字段需要是order by的子集或全集,创建细节记录和多个汇总值,返回多个结果集--rollup的使用方法
    select productid,orderID,sum(quantity) as total from [order details]
    group by productid,orderID
    with rollup--cube的使用方法
    select productid,orderID,sum(quantity) as total from [order details]
    group by productid,orderID
    with cube
    --grouping的使用方法
    select productid,grouping(productid),orderID,grouping(orderID),sum(quantity) as total from [order details]
    group by productid,orderID
    with cube
     
    select case when (grouping(od.productid)=1) then '订单合计:'
    else isnull(od.productid,'未知') end as productID,
    case when (grouping(od.orderID)=1) then '产品合计:'
    else isnull(od.orderID,'未知') end as productID,
    sum(od.quantity) from (select convert(varchar(10),productid) as productID,convert(varchar(10),orderID) as orderid,quantity from [order details]) as od
    group by productid,orderID
    with cube
    --compute的使用方法
    select productid,quantity from [order details]
    order by productid
    compute sum(quantity) by productid
      

  4.   

    不合计的就再case 一下
    select
    case when grouping(姓名)=1
    then '合计' else 姓名 end as 姓名,
    sum(语文) as 语文,
    case when grouping(姓名)=1 then null else sum(数学) end as 数学,
    sum(英语) as 英语,
    sum(化学) as 化学
    from tablename
    group by 姓名 with rollup
      

  5.   

    declare @yw int
    declare @sx int
    declare @yy int
    declare @hx int
    declare @test table(
    xm varchar(10),
    yw int,
    sx int,
    yy int,
    hx int)
    insert into @test
    select '张三',88,98,100,97 union all
    select '王五',89,79,98,95 union all
    select '李四',78,88,95,86 union all
    select '王开',85,96,85,95
    set @yw=(select sum(yw) from @test)
    set @sx=(select sum(sx) from @test)
    set @yy=(select sum(yy) from @test)
    set @hx=(select sum(hx) from @test)
    insert into @test
    select '合计',@yw,@sx,@yy,@hx
    select * from @test
      

  6.   

    楼主可以试试如下:
    全合并:
    declare @test table(
    xm varchar(10),
    yw int,
    sx int,
    yy int,
    hx int)
    insert into @test
    select '张三',88,98,100,97 union all
    select '王五',89,79,98,95 union all
    select '李四',78,88,95,86 union all
    select '王开',85,96,85,95--那如果我数学一列不需要合计呐?
    select case when (grouping(xm)=1) then '合计' else xm end as '姓名',
           sum(yw) as '语文',
           sum(sx) as '数学',
           sum(yy) as '英语',
           sum(hx) as '化学'
    from @test
    group by xm
    with rollup
    select * from @test
    union all
    select '合计',sum(yw) as '语文',null,sum(yy) as '英语',sum(hx) as '化学'
    from @test