【问题标题】:SQL DISTINCT values across rows跨行的 SQL DISTINCT 值
【发布时间】:2019-06-12 08:39:10
【问题描述】:

我在列中有重复的agreementnumber 和重复的telephone,我想在列中获得唯一的agreementnumber,它对应的唯一的telephone

我在 SQL 中编写了查询,它给了我唯一的 agreementnumber,但行中的 telephone 是重复的,但我想要唯一的电话号码。

代码:

select agreementnumber,

  max(case when rn = 1 then telephone end) telephone1,

  max(case when rn = 2 then telephone end) telephone2,

  max(case when rn = 3 then telephone end) telephone3,

  max(case when rn = 4 then telephone end) telephone4,

  max(case when rn = 5 then telephone end) telephone5

from
(
  select agreementnumber, telephone,

    row_number() over(partition by agreementnumber order by telephone) rn
  from alternate_mobile 

) src
group by agreementnumber;

我想要以下输出。 col1 和 col2,col3,col4,col4 中的唯一值。

col1 col2 col3 col4`` AGMTNO phone1 phone2 phone3

【问题讨论】:

  • 您想做一些复杂的事情,但似乎不是……给我们您的数据样本和想要的数据集。
  • 也请标记适当的数据库名称。
  • 我认为这个例子,在这种情况下,虽然总是很受欢迎,但并不是完全必要的。很明显,他想将前 5 部电话分成 5 列,丢弃其余部分,并删除那些相等的部分。您可以准备一组基本数据进行测试。
  • AGREEMENTNUMBER TELEPHONE TN3000CD0027934 8608766694 TN3000CD0027934 8608766694 AP3019CD0012936 9701256775 AP3075CD0029548 9701256775 AP3075CD0029548 9382615863 AP3075CD0029548 8768127359这是样本数据。跨度>

标签: sql


【解决方案1】:

尝试在您的查询中进行这个小改动:

select agreementnumber,
  max(case when rn = 1 then telephone end) telephone1,
  max(case when rn = 2 then telephone end) telephone2,   
  max(case when rn = 3 then telephone end) telephone3,
  max(case when rn = 4 then telephone end) telephone4,
  max(case when rn = 5 then telephone end) telephone5

from
(
  select x.*,
    row_number() over(partition by x.agreementnumber order by x.telephone) rn
  from (
    select distinct agreementnumber, telephone
    from alternate_mobile 
  ) x
) src
group by agreementnumber;

如果您收到重复电话是因为您在 alternate_mobile 表中重复了 agreementnumber/telephone

编辑:

我将查询更改为只保留电话中的数字,删除所有其余字符:

select agreementnumber,
  max(case when rn = 1 then telephone end) telephone1,
  max(case when rn = 2 then telephone end) telephone2,   
  max(case when rn = 3 then telephone end) telephone3,
  max(case when rn = 4 then telephone end) telephone4,
  max(case when rn = 5 then telephone end) telephone5

from
(
  select x.*,
    row_number() over(partition by x.agreementnumber order by x.telephone) rn
  from (
    select distinct agreementnumber, regexp_replace(telephone,'[^0-9]', '') as telephone
    from alternate_mobile 
  ) x
) src
group by agreementnumber;

【讨论】:

  • 我尝试运行代码,但在alternate_mobile 的行中由于缺少表达式而出现错误。我的数据库是 plsql
  • 对不起,子查询中多了一个逗号,现在试试
  • 我需要更多帮助。在我的数据中,一些手机号码的格式不同,如 +91456235674、+91(456)235674 等。数字中也有特殊字符。如何通过删除所有字符将这些仅转换为数字。
  • 您可以使用正则表达式将所有非数字字符替换为''。您必须在非常内部的子查询(“不同”的子查询)中执行此操作
  • 请提供修改后的查询。
【解决方案2】:

请注意,您可以使用rank() 而不是row_number() 来减少子查询的数量:

select agreementnumber,
       max(case when rn = 1 then telephone end) as telephone1,
       max(case when rn = 2 then telephone end) as telephone2,   
       max(case when rn = 3 then telephone end) as telephone3,
       max(case when rn = 4 then telephone end) as telephone4,
       max(case when rn = 5 then telephone end) as telephone5
from (select am.*,
             rank() over (partition by am.agreementnumber order by am.telephone) as rn
      from alternate_mobile am
     ) am
group by agreementnumber;

【讨论】:

  • 嗨@GordonLinoff。完全同意,我只是尝试尽可能少地修改他的查询,以便让他更好地识别他的查询出了什么问题
  • 您好,部分手机号码以 91 开头,导致手机号码为 12 位。如何解决这个问题。请提出建议。
  • @vishal 。 . .新问题应该作为问题提出,而不是在 cmets 中。这个问题已经回答了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 2018-11-25
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多