尝试:
select string, (cnt_2/total::float)*100 perc_admfrom ( select string, count(*) total, sum(case when adm = 2 then 1 else 0 end) cnt_2 from tbl group by string) xorder by string
在这里,
total::float将除法转换为一个
float值,否则
int/int ==>int。
sql小提琴演示

尝试:
select string, (cnt_2/total::float)*100 perc_admfrom ( select string, count(*) total, sum(case when adm = 2 then 1 else 0 end) cnt_2 from tbl group by string) xorder by string
在这里,
total::float将除法转换为一个
float值,否则
int/int ==>int。
sql小提琴演示