【发布时间】:2021-11-11 22:06:23
【问题描述】:
我正在使用 PostgreSQL
我有一张包含潜在客户信息的表格,如下所示:
ID | Phone | link
1 | 3105637247 | https://link_for_id_1
2 | 3105637247 | https://link_for_id_2
3 | 3105637247 | https://link_for_id_3
4 | 3153333651 | https://link_for_id_4
5 | 3153333651 | https://link_for_id_5
目的是使用这些数据向销售团队报告。但要求是不能有重复的电话号码。在这种情况下,我想要的输出是:
ID | Phone | link
1 | 3105637247 | https://link_for_id_1
5 | 3153333651 | https://link_for_id_5
为此,我使用了这个查询:
SELECT DISTINCT Phone,
max(ID), -- Here using aggregated functions to allow ID and link to visualize
max(link)
FROM MyTable
但这种方法有时会提供与我的 ID 不对应的链接:
ID | Phone | link
1 | 3105637247 | https://link_for_id_3
5 | 3153333651 | https://link_for_id_4
有没有办法在 ID 和 Link 对应同一条记录的条件下检索唯一的 Phones?
【问题讨论】:
-
你怎么知道什么是正确的链接?
-
我的意思是 3105637247 的正确链接是什么,为什么?
-
嗨@LeandroBardelli,您可以看到链接的最后一个字符与ID 相同。这就是我知道正确结果的方式。
-
我知道,但是为什么要“删除” 2 和 3?您可以轻松地说正确的链接是 3。
-
例如:为什么是
1 | 3105637247 | https://link_for_id_1而不是3 | 3105637247 | https://link_for_id_3
标签: sql postgresql distinct