求SQL语句,按照不同条件分类统计想求一条sql 语句,统计某个字段数值之和,以及所占百分比,比如
求SQL语句,按照不同条件分类统计想求一条sql语句,统计某个字段数值之和,以及所占百分比,比如有一张表T1,PIDFLAGWEIGHTa120b030c010d1150...
求SQL语句,按照不同条件分类统计想求一条sql 语句,统计某个字段数值之和,以及所占百分比,比如有一张表T1,
PID FLAG WEIGHT
a 1 20
b 0 30
c 0 10
d 1 150
希望结果是
FLAG0 PER0 FLAG1 PER1
30 15% 85% 170
怎么写? 展开
PID FLAG WEIGHT
a 1 20
b 0 30
c 0 10
d 1 150
希望结果是
FLAG0 PER0 FLAG1 PER1
30 15% 85% 170
怎么写? 展开
2个回答
展开全部
靠 你问题能在坑点吗 第一个数据 (pid)c对应的(weight)是0
我说怎么看怎么不对呢
select
sum( case when flag = '0' then weight else 0 end) as FLAG0,
sum( case when flag = '0' then weight else 0 end)/sum(weight) as PER0,
sum( case when flag = '1' then weight else 0 end) as FLAG1,
sum( case when flag = '1' then weight else 0 end)/sum(weight) as PER1
from tab1
另外:如果需要转换成百分数 可以找一下方法 或者问我也行
展开全部
with t2 as (select flag, sum(weight) sum from t1 group by flag)
select sum(case when flag=0 then sum end) flag0, sum(case when flag=0 then sum end)/(select sum(sum) from t2) per0, sum(case when flag=1 then sum end) flag1, sum(case when flag=1 then sum end)/(select sum(sum) from t2) per1 from t2
select sum(case when flag=0 then sum end) flag0, sum(case when flag=0 then sum end)/(select sum(sum) from t2) per0, sum(case when flag=1 then sum end) flag1, sum(case when flag=1 then sum end)/(select sum(sum) from t2) per1 from t2
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询