sqlver中计算百分⽐
初步使⽤sql rver计算取出的数据的百分⽐:
已有条件数据表1张:dati_record 表。其中包含tm_id(每个题⽬的ID),right_anr(正确答案),mem_anr(⽤户给出的答案)
交往>大白菜功效
思路:①⾸先取出每道题有多少⼈答过(构成答题次数,被除数)
好的开始电影②然后取出每道题有多少⼈答对过(构成除数)
③结合答题情况表和回答正确表,联合两张表,获取需要的tm_id、答题次数以及回答正确次数
彷徨读音
④使⽤除法,来获取正确率
-----每道题的答题情况花旗参是西洋参吗
lect tm_id,count(1) as '答题次数'
from dati_record group by tm_id order by '答题次数' desc
-----每道题回答正确情况
lect tm_id,count(1) as '正确次数' from dati_record
春天来了的句子where right_answer = mem_answer group by tm_id order by '正确次数' desc
//⽅法⼀钟鼎文
------- CONVERT(decimal(18, 2),除数 * 1.0 / 被除数) * 100) 取的商结果是四舍五⼊的,并且⼩数部分都是零
_id,b.正确次数,a.答题次数, CAST((CONVERT(decimal(18, 2),b.正确次数 * 1.0 / a.答题次数) * 100) as varchar(50)) +'%' as '正确率'
from ( (lect tm_id,count(1) as '答题次数' from dati_record group by tm_id) a inner join (lect tm_id,count(1) as '正确次数' from dati_record where right_ answer = mem_answer group by tm_id) b _id = b.tm_id )
order by '正确率'
//⽅法⼆
-
--- CAST(CAST(除数*1.0*100/被除数 as decimal(10,2)) as varchar(50)) +'%' 得到的是有零有整的⼩数,不会对结果进⾏四舍五⼊
_id,b.正确次数,a.答题次数, CAST(CAST(b.正确次数*1.0*100/a.答题次数 as decimal(10,2)) as varchar(50)) +'%' as '正确率'
from ( (lect tm_id,count(1) as '答题次数' from game_jili_tsg_record group by tm_id) a inner join (lect tm_id,count(1) as '正确次数' from game_jili_tsg_ record where right_answer = mem_answer group by tm_id) b _id = b.tm_id )
order by '正确率'
关于decimal(a,b)函数玩具兵大战5
decimal(a,b)
参数a ⽤于指定⼩数点左边和右边可以存储的⼗进制数字的最⼤个数,⽬前最⼤精度为38。
参数b ⽤于指定⼩数点右边可以存储的⼗进制数字的最⼤个数。⼩数位数必须是从 0 到 a之间的值。默认⼩数位数是 0。
本次情况,可以学到sql中的CAST()函数,可以参考
关于CONVERT()函数,我⽬前接触的都是 使⽤convert()函数来转换⽇期格式和字符类型。
本次实践中,了解了CONVERT()函数的新⽤法。
接下来会进⼀步了解⼀下两个转换类型函数的区别以及了解为什么有时候使⽤decimal()函数会丢失精度。