如何写百分比的语句

ningweidong 2011-11-03 08:59:58
比如我有一个学生成绩表s
sno score
001 100
002 80
003 60
我会写考多少分的有多少人。
我想看考100分的占总数的百分比,或者看考60分以上的占总数的百分比
这个语句应该怎么写?我用的是sql server 2000
...全文
100 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengxuan 2011-11-03
  • 打赏
  • 举报
回复

if object_id('tb','u') is not null
drop table tb
go
create table tb
(
sno varchar(10),
score int
)
go
insert into tb
select '001',60 union all
select '002',60 union all
select '003',70 union all
select '004',70 union all
select '005',100 union all
select '006',100 union all
select '007',100
go
declare @cnt int
select @cnt=count(*) from tb
select [60分所占比例]=(select count(*)*100/@cnt from tb where score=60),
[70分所占比例]=(select count(*)*100/@cnt from tb where score=70),
[100分所占比例]=(select count(*)*100/@cnt from tb where score=100)
/*
60分所占比例 70分所占比例 100分所占比例
----------- ----------- -----------
28 28 42

(1 行受影响)
*/
jxjdzwang5555 2011-11-03
  • 打赏
  • 举报
回复
declare @tb table
(sno int,score int)
declare @i int,@sum int,@count decimal(18,4)
insert @tb (sno,score) select 1,100 union select 2 ,80 union select 3,60
select @i=count(*) from @tb where score>60
select @sum=count(*) from @tb
select @count=@i/@sum
select convert(varchar(20),@count*100)+'%')
David8977 2011-11-03
  • 打赏
  • 举报
回复
declare @tb table
(sno int,score int)
declare @i decimal(18,4),@sum decimal(18,4),@count decimal(18,4)
insert @tb (sno,score) select 001,100 union select 002 ,80 union select 003,60
select @i=count(*) from @tb where score>60
select @sum=count(*) from @tb
select @count=@i/@sum
select convert(varchar(20),@count*100)+'%'
jxjdzwang5555 2011-11-03
  • 打赏
  • 举报
回复
可以declare @my100 decimal
set @my100 = (sele count(*) from tt where score=100)/(sele count(*) from tt )
吗?
jxjdzwang5555 2011-11-03
  • 打赏
  • 举报
回复
vfp:
use tt
count(*) to lnzong
count(*) to ln100 where score=100
lbfb= ln100/lnzong
?lbfb

我的sql代码:
declare @my100 decimal
set @my100 = (sele count(*) from tt where score=100)/(sele count(*) from tt )
/* i donot how to write */

gw6328 2011-11-03
  • 打赏
  • 举报
回复

declare @t table(sno int,score int);
insert into @t select 1,100 union all
select 2,98 union all
select 3,60 union all
select 4,65;

select score/10*10,COUNT(1)*1.0/(select COUNT(1) from @t) from @t group by score/10
--小F-- 2011-11-03
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-11-03 09:13:21
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([sno] varchar(3),[score] int)
insert [tb]
select '001',100 union all
select '002',80 union all
select '003',60
--------------开始查询--------------------------
select
ltrim(cast( sum(case score when 100 then 1 else 0 end)*100.0/(select count(1) from tb) as dec(18,2)))+'%' as [100分的百分比],
ltrim(cast( sum(case when score>=60 then 1 else 0 end)*100.0/(select count(1) from tb) as dec(18,2)))+'%' as [60分以上的百分比]
from
tb
----------------结果----------------------------
/* 100分的百分比 60分以上的百分比
------------------------------------------ ------------------------------------------
33.33% 100.00%

(1 行受影响)

*/
David8977 2011-11-03
  • 打赏
  • 举报
回复
select (select count(*) from #tb where score>60)/(select count(*) from #tb) 
--小F-- 2011-11-03
  • 打赏
  • 举报
回复
select ltrim(cast( sum(case score when 100 then 1 else 0 end)*100.0/(select count(1) from tb) as dec(18,2)))+'%' from tb

34,590

社区成员

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

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