2024年10月sql如何计算所占比例(SQL 计算表中元素占总量百分比)
⑴sql如何计算所占比例(SQL计算表中元素占总量百分比
⑵SQL计算表中元素占总量百分比
⑶select?疾病类型,疾病名称,SUM(case?when?性别=’男’?then??else??end)?男,???????round(sum(case?when?性别=’男’?then??else??end)*./count(*),)?男性占百分比,???????SUM(case?when?性别=’女’?then??else??end)?女,???????round(sum(case?when?性别=’女’?then??else??end)*./count(*),)?女性占百分比,???????count(*)?总人数from?表名group?by?疾病类型,疾病名称
⑷怎么用SQL语句求百分比
⑸有点不明白、若针对每行求百分比:selectSA/TotelTime,SB/TotelTime,SC/TotelTime,SD/TotelTime,SE/TotelTimefrom表名、若是对总计后的值求百分比:selectsum(SA)/sum(TotelTime),sum(SB)/sum(TotelTime),sum(SC)/sum(TotelTime),sum(SD)/sum(TotelTime),sum(SE)/sum(TotelTime)from表名、当然,以上都是以小数形式显示结果,若要以百分比形式显示结果:乘以,并保留两位小数,然后加上“%”即可。
⑹SQL语句计算男女员工各占比例
⑺假设表员工档案里有性别、姓名两个字段selectcount(*as员工总数,sum(casewhen性别=男thenelseend)男员工数,sum(casewhen性别=男thenelseend)/nullif(count(*,)男所占比例,sum(casewhen性别=女thenelseend)女员工数,sum(casewhen性别=女thenelseend)/nullif(count(*,)女所占比例from员工档案你自己试试看吧楼上的写的不错,但是最好注意一些除问题,要是张空表会报除错误的。
⑻t.province,
⑼cast?(?count?(??)?*??/?(?select?count?(??)?from?province?)?as?varchar?(??)?)?+?’%’
⑽province?t
⑾t.province
⑿sqlserver写法创建表create?table?test(id?int,name?varchar())insert?into?test?values?(,’a’)insert?into?test?values?(,’a’)insert?into?test?values?(,’a’)insert?into?test?values?(,’a’)insert?into?test?values?(,’a’)执行select?a.id,a.count,(a.count+.)/b.countfrom(select?id,count(*)?count?from?test?group?by?id)?a,(select?count(*)?count?from?test)?b
⒀declaresumNumdecimal(,)setsumNum=selectsum(selectgroupby
⒁你这个表示通过SQL语句得来的还是表本来就是这样的???占比的公式就是Y舱/总数C舱/总数F舱/总数如果是通过SQL语句的得来的把SQL语句贴出来!!如果不是表本来就是这样的结构的话,建议你建个视图就行了下面是SQL语句的查询部分select日期,Y舱,Y舱/总数asY舱占比,C舱,C舱/总数asC舱占比,F舱,F舱/总数asF舱占比,总数from表名就行了
⒂用sql语句怎么计算百分比的数值呢求代码!
⒃--建立测试表?testTable
⒄create?table?testTable(id?int?identity(,),type?nvarchar())
⒅--循环插入测试语句
⒆declare?a?int
⒇while?(a《)
⒈insert?into?testTable(type)values(’会员’)
⒉insert?into?testTable(type)values(’vip’)
⒊set?a=a+
⒋--查看插入表中的所有数据
⒌select?*?from?testTable
⒍--查看你要看的的数据
⒎select?b.sumb%a.suma?’会员占总人数的百分比(%)’
⒏from(select?COUNT(*)?suma?from?testTable)?a,
⒐(select?COUNT(*)?sumb?from?testTable?where?type=’会员’)?b
⒑select?b.sumb%a.suma?’vip占总人数的百分比(%)’
⒒from(select?COUNT(*)?suma?from?testTable)?a,
⒓(select?COUNT(*)?sumb?from?testTable?where?type=’vip’)?b
⒔select?b.sumb%a.suma?’会员占总人数的百分比(%)’,c.sumc%a.suma’vip占总人数的百分比(%)’
⒕from(select?COUNT(*)?suma?from?testTable)?a,
⒖(select?COUNT(*)?sumb?from?testTable?where?type=’会员’)?b,
⒗(select?COUNT(*)?sumc?from?testTable?where?type=’vip’)?c
⒘--查看完删除测试表?testTable
⒙drop?table?testTable
⒚结果我已经看过了,如下图:
⒛SQL怎么统计百分比
declarebiXiuint,ZongshuintselectbiXiu=count(*)fromcoursewheretype=’必修’selectZongshu=count(*)fromcourseselectcast(biXiu*./Zongshuasvarchar)+’%’--其中biXiu代表必修个总数--补充字符串说明SELECTCAST(’.’asdecimal)将会得到(小数点后面的将会被省略掉。如果希望得到小数点后面的两位。则需要把上面的改为SELECTCAST(’.’asdecimal(,))这样可以么?
SQL求百分比的问题
这是什么数据库呀?俺没有见过(糗).最后一列能不能直接使用字符串’.%’呢?希望下边的sql能帮助到你SELECTT.供应商,T.货物,T.数量,T.总数量,ROUND(T.数量*/T.总数量,)||’%’比例,’.%’,T.总数量/T.总数量FROM((SELECT供应商,货物,数量FROMT_TABLE)TLEFTJOIN(SELECT货物,count(数量)总数量FROMT_TABLEGROUP货物)TONT.货物=T.货物)