简体   繁体   English

SQL、label用户基于相似度

[英]SQL, label user based on the similarity

Is below case possible in SQL? SQL 中可能出现以下情况吗?

Let say I have a table like this:假设我有一张这样的桌子:

user_id用户身份 product_id产品编号
1 1个 123 123
1 1个 122 122
1 1个 121 121
2 2个 124 124
2 2个 125 125
2 2个 121 121
3 3个 123 123
3 3个 122 122
3 3个 122 122
4 4个 123 123
4 4个 212 212
4 4个 222 222
5 5个 124 124
5 5个 125 125
5 5个 121 121

I want to label the user if they have same product_id, regardless the order, so the output looks like this:我想要 label 用户,如果他们有相同的 product_id,不管顺序如何,所以 output 看起来像这样:

user_id用户身份 product_id产品编号 label label
1 1个 123 123 a一种
1 1个 122 122 a一种
1 1个 121 121 a一种
2 2个 124 124 b b
2 2个 125 125 b b
2 2个 121 121 b b
3 3个 123 123 a一种
3 3个 121 121 a一种
3 3个 122 122 a一种
4 4个 123 123 c c
4 4个 212 212 c c
4 4个 222 222 c c
5 5个 124 124 b b
5 5个 125 125 b b
5 5个 121 121 b b

Please advise请指教

You can use the string_agg function to get the list of product_ids for each user (as a single string), then use the dense_rank function on that string to get unique labels for each product_ids list.您可以使用string_agg function 获取每个用户的 product_ids 列表(作为单个字符串),然后对该字符串使用dense_rank function 以获取每个 product_ids 列表的唯一标签。

select T.user_id, T.product_id, D.label
from table_name T join
(
  select user_id, 
    chr(dense_rank() over (order by user_products) + 96) label
  from
  (  
   select user_id, 
    string_agg(cast(product_id as string), ',' order by product_id)  user_products
   from table_name
   group by user_id
  ) lbl
) D
on T.user_id = D.user_id
order by T.user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM