首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于 compute 的有关问题

2012-01-08 
关于 compute 的问题关于compute的问题:sql查询:selectcustomernameasname,product,moneyfromcustomercomp

关于 compute 的问题
关于   compute   的问题:

sql   查询:     select   customername   as   name,product,money   from   customer   compute   sum(money)    

问题是如何把   sum(money)   的值赋予某个变量(或者字段),以便在页面显示?


(或另有可代替的   sql   语句也行,要求是:   查询   customername,product,money,   并对   money   求和,   money   的和要能够调用)




[解决办法]
select customername name,product,money from customer
union all
select '总计 ', ' ',sum(money) from customer

[解决办法]
select a.customername as name ,a.product,a.money,b.money as sum_money
from customer a
left join (select sum(money) money from customer) b
on 1=1
[解决办法]
declare @i as decimal(18,2)

select @i = money from customer compute sum(money)

select @i
[解决办法]

select name,volume from tt union all
select '总计 ',sum(volume) from tt
--赋值变量
declare @i decimal
select @i=sum(volume) from tt
print @i
[解决办法]
declare
@i int
select customername,product,money from customer
union
select @i=sum(money)from customer
[解决办法]
如果想把兩個結果放到一個查詢裡,這麼寫

select customername as name,product,SUM(money) As money from customer Group By customername,product With Rollup Having customername Is Null Or product Is Not Null

[解决办法]
Create Table customer
(customernameVarchar(10),
productVarchar(10),
[money]Money)
Insert customer Select 'A ', 'A1 ', 200
Union All Select 'A ', 'A2 ', 300
Union All Select 'B ', 'B1 ', 200
GO
select customername as name,product,SUM(money) As money from customer Group By customername,product With Rollup Having customername Is Null Or product Is Not Null
GO
Drop Table customer
--Result
/*
nameproductmoney
AA1200.0000
AA2300.0000
BB1200.0000
NULLNULL700.0000
*/

热点排行