情况如下:
现有表P 字段有 sheet_no ,money,flag
里面的数据大致如下,需要统计营业额  
--------------------------------
sheet_no    money      flag
A1      200.0000   A
A1      200.0000   B
A2      400.0000   A
A2     400.0000   B
A3      600.0000   A
--------------------------------
我现在用
select distinct sheet_no,money
from p
compute sum(money)
生成的结果集,不知道怎么把compute的结果 赋值 给变量?
我现在是通过临时表中转解决的,想请问怎么将compute生成的赋给变量,或者有类似别的办法直接解决的?----------------------------------------------
declare   @i   as   decimal(18,2) 
select   @i   =   money   from  p compute   sum(money)   
select   @i提示错误:
在 赋值 中不允许使用 COMPUTE 子句。
-----------------------------------------------

解决方案 »

  1.   


    declare       @i       as       decimal(18,2)  
    select @i = sum(money)  from p 
      

  2.   

    select sum(money)  from
    (select distinct  sheet_no,money   from  @t) a
    or select sum(distinct money) from @t
      

  3.   


    declare @i as decimal(18,2)select @i = sum(money)  from
    (select distinct  sheet_no,money   from  @t) a
    select @ior select @i = sum(distinct money) from @tselect @i
      

  4.   


    select @i = sum(money)  from
    (select distinct  sheet_no,money   from  @t) a
    select @i
    我现在用的就是类似这样的临时表的方式
    谢谢了..
    select @i = sum(distinct money) from @t
    表的数据很多的,蛮多订单的价格都是一样的..
    刚才描述的不够详细了..继续等待..
      

  5.   


    select sum(money) 
    from @t t
    where not exists(select 1 from @t where sheet_no = t.sheet_no and flag> t.flag)
      

  6.   

    declare @t table(sheet_no varchar(2),[money] numeric(12,2),flag char(1))
    insert @t select 'A1',200.0000,'A' 
    insert @t select 'A1',200.0000,'B' 
    insert @t select 'A2',400.0000,'A' 
    insert @t select 'A2',400.0000,'B' 
    insert @t select 'A3',600.0000,'A'
    -------------------------------- select   distinct   sheet_no,money 
    from  @tcompute sum(money) select sum(money)  from
    (select distinct  sheet_no,money   from  @t) a
    select sum(money) 
    from @t t
    where not exists(select 1 from @t where sheet_no = t.sheet_no and flag> t.flag)
    /*
    sheet_no money          
    -------- -------------- 
    A1       200.00
    A2       400.00
    A3       600.00         sum
             ========================================
             1200.00
    (所影响的行数为 4 行)                                         
    ---------------------------------------- 
    1200.00(所影响的行数为 1 行)                                         
    ---------------------------------------- 
    1200.00(所影响的行数为 1 行)
    8/
      

  7.   

    恩 谢谢  
    如果在存储过程中
    能不能把
    compute 生成的结果
    赋给某个变量呢? 
      

  8.   

    恩   谢谢     
    如果在存储过程中 
    能不能把 
    compute   生成的结果 
    赋给某个变量呢?   
    ---我还没有找到办法,帮助也没有