SQLserver一个表查询单个字段在这个字段的占比

select t1.brand 品牌,

t1.disc as disc,t1.price as price,t1.val as val,t1.qty as qty ,

Convert(decimal(20,2),t1.qty/t3.totalsell_qty*100) as qtypercent,//计算qty所占百分比

Convert(decimal(20,2),t1.val/t4.totalsell_val*100) as valpercent //计算val所占百分比

from (select brand,sum(sell_qty) qty,sum(sell_val) val,Convert(decimal(20,2),Avg(disc_rate)) as disc, Convert(decimal(20,2),Sum(unit_price)) as price from tbl_crm_rlb group by brand ) t1,//得到unit_price字段集合

(select sum(sell_qty) totalsell_qty from tbl_crm_rlb) t3,//得到sell_qty字段集合

(select sum(sell_val) totalsell_val from tbl_crm_rlb) t4//得到sell_val字段集合

ORDER BY qtypercent DESC

展开阅读全文

没有更多推荐了,返回首页