当前位置: 代码迷 >> Sql Server >> 请问几个超级简单的SQL语句
  详细解决方案

请问几个超级简单的SQL语句

热度:184   发布时间:2016-04-27 21:56:33.0
请教几个超级简单的SQL语句:
1.   有一个表,名称,数量..已经按降序排好了的.
求出它的前面20%的名称的,数量之合.
类似于:

select   top   20   percent   数量from   表名     //这句话可以..
//下面的就不行了.
declare   @departtotal   int  
select   @departtotal   =   sum   (select   top   20   percent   数量from   表名)


------解决方案--------------------
--try

declare @departtotal int
select top 20 percent @departtotal=sum(数量) from 表名
select @departtotal

------解决方案--------------------
试试:
declare @departtotal int
select @departtotal = sum(数量) from (select top 20 percent 数量 from 表名) t
------解决方案--------------------
试试:
declare @departtotal int
select @departtotal = sum(数量) from (select top 20 percent 数量 from 表名) t

-------------
留下一个标记
[email protected] = sum(数量) 和去掉sum有什么区别呢 因为你来自的一个表t里面只有数量 那么选择出来的就是 前20%的数量 结果和 select top 20 percent 数量 from 表名 查询的结果是一样的 不明白,希望解释一下
  相关解决方案