如何对与密钥关联的所有值进行con依?

发布于 2025-01-22 21:16:58 字数 911 浏览 4 评论 0原文

我有以下架构:

CREATE TABLE table1 
(
    user,
    phoneType, --from ['A','B','C','D', 'E'], user can have any number of any type
    uniquePhoneID, --unique string identifying the phone
    day_id --date; record does not necessarily exist for every seen user + phoneType every day, represented as number in example
);

INSERT INTO table1 
VALUES (1, 'A', xyz, 1),
       (1, 'A', abc, 1),
       (1, 'B', def, 2),
       (1, 'A', xyz, 2),
       (1, 'C', hij, 4),
       (1, 'A' xyz, 5),
       (2, 'C', w, 9),
       (2, 'D', z, 10),
       (2, 'A', p, 10),
       (2, 'E', c, 11),
       (3, 'A', r, 19),
       (3, 'B', q, 19),
       (3, 'B', q, 20),
       (3, 'B', f, 20),
       (3, 'B', y, 21); 

一个用户,唯一的day_id最多只会出现一次,但在任何给定的一天都不一定。 我希望按字母顺序排列桌子中的每个用户的4个phoneTypes,因此结果如下:

1 | AABC
2 | ACDE
3 | ABBB

我尝试了几种不同的方法来执行此操作,但我不确定如何获得我正在寻找的答案。

I have the following schema:

CREATE TABLE table1 
(
    user,
    phoneType, --from ['A','B','C','D', 'E'], user can have any number of any type
    uniquePhoneID, --unique string identifying the phone
    day_id --date; record does not necessarily exist for every seen user + phoneType every day, represented as number in example
);

INSERT INTO table1 
VALUES (1, 'A', xyz, 1),
       (1, 'A', abc, 1),
       (1, 'B', def, 2),
       (1, 'A', xyz, 2),
       (1, 'C', hij, 4),
       (1, 'A' xyz, 5),
       (2, 'C', w, 9),
       (2, 'D', z, 10),
       (2, 'A', p, 10),
       (2, 'E', c, 11),
       (3, 'A', r, 19),
       (3, 'B', q, 19),
       (3, 'B', q, 20),
       (3, 'B', f, 20),
       (3, 'B', y, 21); 

A single user, uniquePhoneID, day_id will only show up at most once, but not necessarily at all on any given day.
I am looking to concatenate each user in the table with their 4 phoneTypes in alphabetical order, so the result is as follows:

1 | AABC
2 | ACDE
3 | ABBB

I have tried a few different ways of doing this but I am unsure how to get the answer I am looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2025-01-29 21:16:58

我认为用户是一个保留的单词,因此您必须解决这个问题。否则,我认为这样的事情对您有用:

select user, string_agg (phonetype, '' order by phonetype)
from table1
group by user

- 编辑4/21/2022-

啊,好的。我没有从最初的问题中汲取这一点。

如果您在汇总之前使用了原始表上的不同之处,该怎么办?

select userid, string_agg (phonetype, '' order by phonetype)
from (select distinct userid, phonetype, uniquephoneid from table1) x
group by userid

我从这个版本中获得了这些结果:

1   AABC
2   ACDE
3   ABBB

如果该逻辑仍然不起作用,您可以更改示例数据以找到一个失败的示例吗?

I think user is a reserved word, so you will have to resolve that. Otherwise, I think something like this will work for you:

select user, string_agg (phonetype, '' order by phonetype)
from table1
group by user

-- EDIT 4/21/2022 --

Aah, okay. I did not glean that from the original question.

What if you used the distinct on the original table before the aggregation?

select userid, string_agg (phonetype, '' order by phonetype)
from (select distinct userid, phonetype, uniquephoneid from table1) x
group by userid

I got these results from this version:

1   AABC
2   ACDE
3   ABBB

If that logic still doesn't work, can you alter the sample data to find an example where it fails?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文