一个产品ID有很多价格。我试图通过比较每个价格和产品的中位数价格来找到可能不正确的价格。目前我在输出中有最小值和最大值,并希望为每个产品的中位数价格添加一列。我试过avg()但是这会返回:类型为numeric的输入语法无效。有什么想法吗?
select
distinct dp.product_id,
pc.warehouse_id as retailer_id,
pc.code,
dp.product_name,
dp.product_brand_name,
dp.product_size,
dp.product_unit_count,
pd.product_name as retailer_name,
pd.brand_name as retailer_brand_name,
pd.size as retailer_size,
pd.price,
sp.max_cost,
sp.min_cost
from dim_product as dp
join product_codes as pc on pc.product_id = dp.product_id and pc.deleted_ind = 'N'
join suspect_products as sp on sp.product_id = dp.product_id
left join (
select
nvl(json_extract_path_text(rf."raw",'brand_name'),dei.brand_name) as brand_name,
nvl(json_extract_path_text(rf."raw",'name'),dei.name) as product_name,
nvl(json_extract_path_text(rf."raw",'size'), dei.size) as size,
nvl(json_extract_path_text(rf."raw",'cost_price_per_unit'),dei.cost_price_per_unit::VARCHAR) as price,
nvl(rf.code,dei.lookup_code) as code,
nvl(rf.retailer_id,dei.warehouse_id) as retailer_id
from product_retailer_file_data as rf
left join (
select
d.brand_name,
d.name,
d.size,
d.cost_price_per_unit,
d.lookup_code,
d.warehouse_id
from data_entry_items as d
join (
select lookup_code, max(created_at) as max_date
from data_entry_items
where created_at > current_date-720
group by 1) as m on m.lookup_code = d.lookup_code and m.max_date = d.created_at
) as dei on dei.lookup_code = rf.code and dei.warehouse_id = rf.retailer_id
) as pd on pd.code = pc.code and pd.retailer_id = pc.warehouse_id
order by 1;