• 主页
• 基础类
• 应用实例
• 新技术前沿

# 获取 compute 值的问题

skyfor 2008-01-08 02:02:21

--------------------------------
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)

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

-----------------------------------------------
...全文
65 点赞 收藏 10

10 条回复

skyfor 2008-01-09

-狙击手- 2008-01-08

compute 生成的结果

---

skyfor 2008-01-08

compute 生成的结果

-狙击手- 2008-01-08
``````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  @t

compute 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/``````

-狙击手- 2008-01-08
``````
select sum(money)
from @t t
where not exists(select 1 from @t where sheet_no = t.sheet_no and flag> t.flag)``````

skyfor 2008-01-08
``````
select @i = sum(money)  from
(select distinct  sheet_no,money   from  @t) a
select @i
``````

``````
select @i = sum(distinct money) from @t
``````

-狙击手- 2008-01-08
``````
declare @i as decimal(18,2)

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

or

select @i = sum(distinct money) from @t

select @i

``````

-狙击手- 2008-01-08
``````select sum(money)  from
(select distinct  sheet_no,money   from  @t) a

or

select sum(distinct money) from @t``````

tim_spac 2008-01-08
``````
declare       @i       as       decimal(18,2)
select @i = sum(money)  from p
``````

skyfor 2008-01-08

MS-SQL Server

3.2w+

MS-SQL Server相关内容讨论专区