求助 统计的sql语句

lvxianda 2011-03-23 12:03:49
求个sql语句

ID 类别
1 A
2 A
3 A
4 B
5 B
6 B
7 C
8 A
9 D
10 C
如何知道各个类别占总数的百分比
...全文
144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-03-23
  • 打赏
  • 举报
回复
select
[类别],ltrim(cast(count(1)*100.0/(select count(1) from [表]) as decimal(18,2)))+'%' as 百分比
from
[表]
group by
[类别]
gw6328 2011-03-23
  • 打赏
  • 举报
回复
over()怎么搞的哦?
AcHerat 元老 2011-03-23
  • 打赏
  • 举报
回复

select 类别,ltrim(cast(count(1)*100.0/count(1)over() as decimal(6,2))) + '%' as [百分比]
from tb
group by 类别
AcHerat 元老 2011-03-23
  • 打赏
  • 举报
回复

select
[类别],ltrim(cast(count(1)*100./(select count(1) from [表]) as decimal(6,2)))+'%' as 百分比
from
[表]
group by
[类别]
lvxianda 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ssp2009 的回复:]
SQL code
select 类别,count(1)*1.0/count(1)over() from tb group by 类别
[/Quote]

这个结果好像有问题 我想要的结果是A 40% B30% C20% D10%
lvxianda 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fredrickhu 的回复:]
SQL code
select
[类别],ltrim(count(1)/(select count(1) from [表]))+'%' as 百分比
from
[表]
group by
[类别]
[/Quote]

统计出来的结果都是0%
--小F-- 2011-03-23
  • 打赏
  • 举报
回复
select
[类别],ltrim(count(1)/(select count(1) from [表]))+'%' as 百分比
from
[表]
group by
[类别]
jwdream2008 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ssp2009 的回复:]
SQL code
select 类别,count(1)*1.0/count(1)over() from tb group by 类别
[/Quote]
天-笑 2011-03-23
  • 打赏
  • 举报
回复


select [类别],count(1)/(select count(1) from [表])
from [表]
group by [类别]

快溜 2011-03-23
  • 打赏
  • 举报
回复
select 类别,count(1)*1.0/count(1)over() from tb group by 类别

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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