按另一列过滤一列中的重复记录 SQL Netezza

问题描述

我有一个表,其中一行包含重复值:[A_Number] 但其他的不是,所以我需要使用另一个字段过滤这些重复记录:[Area_code],但是 [A_Number]并不总是有重复的值,

使用以下示例:

区域代码 A_Number
955 2324356
55 2324356
945 2324356
45 2324356
940 8675643
13 4450987
  • 问题是:由于 A_NumberArea_Code 可能有重复的记录,每个重复的 A_Number 都有 2 个 Area_Code,其中一个以 9 开头,并且有 3 个数字但另一个没有 9 并且有 2 位数字,所以我需要获取没有 9 的 Area_Code 并且只有 2 位数字。
  • 如果 A_Number 有一个以 9 开​​头且有 3 个数字的 Area_Code,我们将从 9 中删除 Area_Code
  • 如果 A_Number A_Number 有一个 Area_Code,没有 9 和有 2 个数字将是相同的
  • [编辑] A_Number 可以有不同的 Area_Code,如 A_Number:2324356

预期结果

区域代码 A_Number
55 2324356
45 2324356
40 8675643
13 4450987

解决方法

如果 area_code 将始终重复最后两位数字,即 9xxxx(其中 xx 在所有出现中都相同),那么具有适当子字符串的简单分组将起作用出 -

select a_number,case 
                   when area_code like '9%' 
                     then substring(area_code,2)
                   else area_code
                 end as code
from t
group by a_number,code

但是,如果 xx 是不同的数字,那么您必须选择如何将它们限制为您想要的数字

-- take only the first (min) or last (max)
select a_number,min(code) as first_code,max(code) as last_code
from 
    select a_number,2)
                   else area_code
                 end as code
    from t
    group by a_number,code ) tmp
group by a_number
,

这回答了问题的原始版本。

认为你基本上想要 min() 带有一些字符串解析逻辑:

select a_number,(case when min(area_code) like '9%'
             then substring(min(area_code),2)
             else min(area_code)
        end)
from t
group by a_number;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...