SQL中位数
表:sku_info
字段:sku_id name category_id price
需求:求各分类商品的价格中位数
期望结果:
category_id medprice
1 select 2 category_id 3 ,cast(avg(if(ct/2 = 0,if(num = ct / 2 or num = ct / 2 + 1, price, null ),if(num = ct / 2 + 1, price, null ))) ) as decimal(16,2) 4 5 from ( 6 select category_id,price,row_number() over(partition by category_id order by price) num 7 ,count(*) over(partition by category_id) ct from sku_info 8 ) a group by category_id
浙公网安备 33010602011771号