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

求两个sql...
一.如何用行号除以总行数,如:
行号 计算
1 20%
2 40%
3 60%
4 80%
5 100%
二.累计每行的和
数量 合计  
100 100
200 300
300 600
400 1000

谢谢了...


------解决方案--------------------
SQL code

declare @t table (id int)
insert into @t
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5

select id,ltrim(id*100/(select count(1) from @t))+'%' as 计算  from @t
/*
id          计算
----------- -------------
1           20%
2           40%
3           60%
4           80%
5           100%
*/

------解决方案--------------------
SQL code

declare @t table (数量 int)
insert into @t
select 100 union all
select 200 union all
select 300 union all
select 400

select *,(select sum(数量) from @t where 数量<=t.数量) as 合计 from @t t
/*
数量          合计
----------- -----------
100         100
200         300
300         600
400         1000
*/