最优方法!!!

AcHerat 2010-12-07 11:35:25
表 delivery

`````customer`````````amount
------------------------------
````````1```````````````10
````````1```````````````40
````````2```````````````23
````````3```````````````56
````````3```````````````35
````````4```````````````6

结果显示:

``````客户`````````总额```````````百分比
-----------------------------------------
```````1````````````50`````````````29.4% ==== (10+40)/(10+40+23+56+35+6)
……………………

测试代码!
...全文
82 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
水妹妹 2010-12-07
  • 打赏
  • 举报
回复
select 客户,总额=sum(amount),
百分比=ltrim(cast(总额*100.0/(select sum(amount) from tb) as decimal(10,1)))+'%'
from tb
group by 客户
水族杰纶 2010-12-07
  • 打赏
  • 举报
回复
if not object_id('tb') is null
drop table tb
Go
Create table tb([customer] int,[amount] int)
Insert tb
select 1,10 union all
select 1,40 union all
select 2,23 union all
select 3,56 union all
select 3,35 union all
select 4,6
Go
Select [customer],
sum([amount])总额,
ltrim(cast(sum([amount])*100.0/(select sum([amount])from tb) as dec(18,2)))+'%'百分比
from tb
group by [customer]
/*
customer 总额 百分比
----------- ----------- ------------------------------------------
1 50 29.41%
2 23 13.53%
3 91 53.53%
4 6 3.53%
*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧