如何获得以下数据集的不同 parent_sku 计数?

How to get distinct parent_sku count for the below dataset?

需要您的帮助以从以下数据集中提取不同的 Parent_sku 计数。

条件:如果 parent_sku 中的一个 child 具有“高”sales_tag,则 parent_sku 计数应从“低”sales_tag 中排除.

P.S。 Sales_tag 列基于 child_sku 列。

感谢您的帮助。

Dateset_&_Output

create temp table pb_sku_high as 
(
Select brand
,sales_tag
,count(distinct child_sku) as child_sku_count
,count(distinct parent_sku) as parent_sku_count
from pb_sku_base
Where sales_tag = 'High'
group by 1,2
);

drop table if exists pb_sku_low;

create temp table pb_sku_low as 
(
Select brand
,sales_tag
,count(distinct child_sku) as child_sku_count
,(select count(distinct parent_sku) from pb_sku_base 
        where parent_sku not in 
                           (
                            select parent_sku from pb_asins_base where sales_tag = 'High' group by 1
                           )
    ) as parent_sku_count 
from pb_asins_base
Where sales_tag = 'High'
group by 1,2
);

Select * from pb_sku_high
union all
select * from pb_sku_low;

呈现的模式是针对此类查询的 non-conducive,但是我们可以规范化结构以提取 parentchild 行基于 Child_sku 值。

然后我们可以LEFT JOIN parentchild行来记录计数.

NOTE:
This specific query will not likely return the correct counts if there are more than 1 single child for each parent, it is not clear from the instructions how that should be treated though, so it mnight give you what you need.

SELECT parent.Brand, CASE WHEN parent.Sales_Tag = 'High' OR child.Sales_Tag = 'High' THEN 'High' ELSE 'Low' END as Sales_Tag, COUNT(*) as P_SKU_Count
FROM Product parent
LEFT JOIN Product child ON parent.Brand = child.Brand AND parent.Parent_sku = child.parent_sku AND parent.Child_Sku <> child.Child_Sku
WHERE parent.Child_sku LIKE '%_C1'
GROUP BY parent.Brand, CASE WHEN parent.Sales_Tag = 'High' OR child.Sales_Tag = 'High' THEN 'High' ELSE 'Low' END
Brand Sales_Tag P_SKU_Count
Nike High 3
Nike Low 2

您还可以避免 GROUP BY 语句中的 CASE 和 return 这两个计数 in-line:

SELECT parent.Brand
     , SUM(CASE WHEN parent.Sales_Tag = 'High' OR child.Sales_Tag = 'High' THEN 1 END) as High
     , SUM(CASE WHEN parent.Sales_Tag = 'High' OR child.Sales_Tag = 'High' THEN 0 ELSE 1 END) as Low
FROM Product parent
LEFT JOIN Product child ON parent.Brand = child.Brand AND parent.Parent_sku = child.parent_sku AND parent.Child_Sku <> child.Child_Sku
WHERE parent.Child_sku LIKE '%_C1'
GROUP BY parent.Brand;
Brand High Low
Nike 3 2

这里有一个数据库 Fiddle 用于测试:https://www.db-fiddle.com/f/s2hDvn8EU3QXcdwKqobhdc/0