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

sql 语句,查询总数解决方案

2012-04-01 
sql 语句,查询总数一张ShopProduct表中有四个字段:ID,CreateTImes,shopID 和 leftAmount我要查询 Sum(left

sql 语句,查询总数
一张ShopProduct表中有四个字段:ID,CreateTImes,shopID 和 leftAmount 
我要查询 Sum(leftAmount)不超过 8的 shopID 和Sum(LeftAmount)
我的语句是:select * from (select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID from ShopProduct as sp where sp.ProductID=238 group by sp.ShopID) as shopP where shopP.Amount<=8  
请问还有没有更优化的语句。

[解决办法]

SQL code
select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID from ShopProduct as sp where sp.ProductID=238 group by sp.ShopIDhaving SUM(sp.LeftAmount)<=8
[解决办法]
select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID from ShopProduct as sp where sp.ProductID=238 group by sp.ShopID having SUM(sp.LeftAmount) < 8

热点排行