去連線幫助裡面查 ROLLUP
WITH ROLLUP

解决方案 »

  1.   

    就是一个竞猜系统
    表的字段有 id ,UserId,Odds,
    我想把Odds的和算出来,然后把每条记录竞猜的Odds取出来,并且只执行一个SQL语句
      

  2.   


    declare @t table
    (
    id int identity(1,1),
    UserId varchar(10),
    Odds int
    )insert @t select 'a',12
    union all select 'b',23
    union all select 'c',34
    select
    isnull(userid,'total') as userid,
    sum(odds) as odds
    from @t
    group by userid
    with rollup/**
    userid  odds
    --------------
    a     12
    b     23
    c     34
    total 69
    **/
      

  3.   

    select sum(Odds) from 表 union all select Odds from 表