求个sql语句(数据库那边人太少了......)
表T
name Type
A a
B a
C b
D b
我想查出a跟b所占的比例各是多少
例如上面显示的结果就应该是:
Type scale
a 50%
b 50%
[解决办法]
先计算出数量,然后在界面上显示比例:
select Max(Type) as Type,Count(Type) as Number from T group by Type
[解决办法]
select'A' name ,'a' [Type] into #tmp unionselect'B','a' unionselect'C','b' unionselect'D','b' select [Type], str(count(*) * 100/ (select count(*) from #tmp)) +'%' as scale from #tmp group by [type] drop table #tmp
[解决办法]
Access下测试成功:
select [Type],count(*)/(select count(*) from ex)*100&'%' as scale from ex group by [Type]
SQL Server下可能要把连接字符的&改成+吧,试下这个
select [Type],count(*)/(select count(*) from ex)*100+'%' as scale from ex group by [Type]
[解决办法]
select type,concat(to_char(count(name)/(select count(*) from T)*100),'%') from T group by type
[解决办法]
select distinct Type,cast((100.*COUNT(1) over(partition by Type)/COUNT(1) over()) as varchar(20))+'%' as scale from 表T;