[英]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 ![]() |
123 ![]() |
1 ![]() |
122 ![]() |
1 ![]() |
121 ![]() |
2 ![]() |
124 ![]() |
2 ![]() |
125 ![]() |
2 ![]() |
121 ![]() |
3 ![]() |
123 ![]() |
3 ![]() |
122 ![]() |
3 ![]() |
122 ![]() |
4 ![]() |
123 ![]() |
4 ![]() |
212 ![]() |
4 ![]() |
222 ![]() |
5 ![]() |
124 ![]() |
5 ![]() |
125 ![]() |
5 ![]() |
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 ![]() |
---|---|---|
1 ![]() |
123 ![]() |
a![]() |
1 ![]() |
122 ![]() |
a![]() |
1 ![]() |
121 ![]() |
a![]() |
2 ![]() |
124 ![]() |
b ![]() |
2 ![]() |
125 ![]() |
b ![]() |
2 ![]() |
121 ![]() |
b ![]() |
3 ![]() |
123 ![]() |
a![]() |
3 ![]() |
121 ![]() |
a![]() |
3 ![]() |
122 ![]() |
a![]() |
4 ![]() |
123 ![]() |
c ![]() |
4 ![]() |
212 ![]() |
c ![]() |
4 ![]() |
222 ![]() |
c ![]() |
5 ![]() |
124 ![]() |
b ![]() |
5 ![]() |
125 ![]() |
b ![]() |
5 ![]() |
121 ![]() |
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.