我想按进一步对应于price_type列的结果集进行分组, 如果同时存在具有price_type变体和parent_product的产品的数据,则该数据必须仅显示变体
例如,此数据
Product Name PPID QTY PRID PRICE PRICE_TYPE
Shorts 1 10 21 200 variant
Shorts 1 10 21 100 parent_product
Night Suit 1 10 22 200 variant
Night Suit 1 10 22 100 parent_product
Denim 1 10 23 400 parent_product
应该像
Product Name PPID QTY PRID PRICE PRICE_TYPE
Shorts 1 10 21 200 variant
Night Suit 1 10 22 200 variant
Denim 1 10 23 400 parent_product
答案 0 :(得分:1)
似乎您想使用{em>有条件排序的row_number()
:
select *
from (select *, row_number() over (partition by ProductName
order by (case when Price_type = 'variant'
then 0 else 1
end)
) as seq
from table
) t
where seq = 1;
答案 1 :(得分:1)
下面是获取所需结果的简单查询。
select distinct on(product_name), t.* from tab t order by price_type='variant' desc
答案 2 :(得分:0)
您可以使用窗口功能:
SELECT * FROM
(
SELECT * ,
RANK() OVER (PARTITION BY product_name ORDER BY priority ) AS rank
FROM (
SELECT *,
CASE
WHEN price_type='variant' THEN 1
ELSE 2
END AS priority
FROM yourtable
) AS X
) AS Y
WHERE rank=1