之前的SQL因为必须把非聚合函数的字段 group by,导致得不到想要的值,却一直没想起可以用sum( ),over(partition by )分组
select z.*, sum(row_num) over(partition by category_one, labeltype, ztlb) total_num
, round(row_num / sum(row_num) over(partition by category_one, labeltype, ztlb), 2) rate_value
from
(
select category_one, labeltype, ztlb
, decode(tzx_evaluation, '-1', '滞销', '0', '平销', '1', '畅销') || '占比' tzx_value
, count(1) row_num
from bi_goods_aistock
where tzx_evaluation is not null
group by category_one, labeltype, ztlb, tzx_evaluation
) z
order by category_one, labeltype, ztlb, tzx_value;