SQLserver一個表查詢單個欄位在這個欄位的佔比
阿新 • • 發佈:2018-12-10
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