读书人

求两个sql.解决思路

发布时间: 2012-03-18 13:55:38 作者: rapoo

求两个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 @tselect 1 union allselect 2 union allselect 3 union allselect 4 union allselect 5select 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 @tselect 100 union allselect 200 union allselect 300 union allselect 400select *,(select sum(数量) from @t where 数量<=t.数量) as 合计 from @t t/*数量          合计----------- -----------100         100200         300300         600400         1000*/ 

读书人网 >SQL Server

热点推荐