SQl查询:对类别中的商品进行计数,一旦商品归入1个类别,就不将其归入其他类别

时间:2019-10-21 12:23:23

标签: mysql sql sql-server

我有一个产品属于不同类别,其中1个产品可能属于1个以上类别。我想得到“哪个类别的产品最独特”

样本数据:

Table: Category

Category_ID ||   NAME 
1           ||   clothes
2           ||   bags
3           ||   shirts


Tabale: category_joins {product + category ids only}
Product_ID ||   Category_ID
1          ||    1
5          ||    1

1          ||    3
2          ||    3  
3          ||    3   

算出哪个类别的商品最独特?

Result
Count     ||   Category_id
1         ||   1
3         ||   3

{Exclude Product_ID "1" from Category_ID "1", because it is in category "3",
and only count Product_ID 1 in Category 3 because Category 3 has most unique products}

产品ID为“ 1”的两个类别

我想要类别中的产品总数, 但是如果产品属于一个类别,则不要将其归入另一个类别。

帮我写它的查询。

1 个答案:

答案 0 :(得分:2)

如果在Category_ID上相等,则类别的是否存在取决于产品的数量。对于每种产品,仅将其放在最高优先级类别。

select Product_ID, Category_ID
from (
    select  c1.Product_ID, c1.Category_ID, row_number() over (partition by Product_ID order by rnk) priority
    from category_joins c1
    join (
      select Category_ID, row_number() over (order by count(distinct Product_ID) desc, Category_ID) rnk
      from category_joins
      group by Category_ID
    ) cr on cr.Category_ID = c1.Category_ID
) t
where priority = 1

Demo

按类别获取唯一分发产品的数量

select Category_ID, count(*) n
from (
    select  c1.Product_ID, c1.Category_ID, row_number() over (partition by Product_ID order by rnk) priority
    from category_joins c1
    join (
      select Category_ID, row_number() over (order by count(distinct Product_ID) desc, Category_ID) rnk
      from category_joins
      group by Category_ID
    ) cr on cr.Category_ID = c1.Category_ID
) t
where priority = 1
group by Category_ID;