日期:2014-05-18  浏览次数:20422 次

说是Vstock_TotalMoney 未在聚合列中 缺少group by ????
select @MoneyAvailable = a.Vstock_TotalMoney - SUM(b.BuyPrice * b.BuyVol) - SUM(b.BuyTax) - SUM(b.BuyFee) 
from dbo.aspnet_Users a 
inner join dbo.aspnet_Vstock_BuyOrder b
on a.UserId = @UserId and b.UserId = a.UserId

怎么加 group by啊?!

另外,上面的可不可以写成 ,都写在一个sum中?!

select @MoneyAvailable = a.Vstock_TotalMoney - SUM(b.BuyPrice * b.BuyVol + b.BuyTax + b.BuyFee) 
from dbo.aspnet_Users a 
inner join dbo.aspnet_Vstock_BuyOrder b
on a.UserId = @UserId and b.UserId = a.UserId

------解决方案--------------------
你可以这样写
select @MoneyAvailable=sum(Vstock_TotalMoney)
from dbo.aspnet_Users

select @MoneyAvailable = @MoneyAvailable - SUM(b.BuyPrice * b.BuyVol) - SUM(b.BuyTax) - SUM(b.BuyFee) 
from dbo.aspnet_Users a 
inner join dbo.aspnet_Vstock_BuyOrder b
on a.UserId = @UserId and b.UserId = a.UserId

------解决方案--------------------
试试这个。
SQL code


select  a.UserId,@MoneyAvailable = SUM(a.Vstock_TotalMoney - (b.BuyPrice * b.BuyVol + b.BuyTax + b.BuyFee) )
from dbo.aspnet_Users a 
inner join dbo.aspnet_Vstock_BuyOrder b
on a.UserId = @UserId and b.UserId = a.UserId
GRoup by  a.UserId

------解决方案--------------------
高手们出马 你的问题会立即解决的 过来学习了
------解决方案--------------------
select @MoneyAvailable=(
a.Vstock_TotalMoney-
(select SUM(b.BuyPrice * b.BuyVol) from dbo.aspnet_Vstock_BuyOrder b where b.UserId = a.UserId)-
(select SUM(b.BuyTax) from dbo.aspnet_Vstock_BuyOrder b where b.UserId = a.UserId)-
(select SUM(b.BuyFee) from dbo.aspnet_Vstock_BuyOrder b where b.UserId = a.UserId)
)
from dbo.aspnet_Users a 
where a.UserId = @UserId

你这个要用子查询来做。
------解决方案--------------------
select @MoneyAvailable=Vstock_TotalMoney
from dbo.aspnet_Users where UserId = @UserId

select @MoneyAvailable = @MoneyAvailable - SUM(b.BuyPrice * b.BuyVol) - SUM(b.BuyTax) - SUM(b.BuyFee) 
from dbo.aspnet_Users a 
inner join dbo.aspnet_Vstock_BuyOrder b
on a.UserId = @UserId and b.UserId = a.UserId