oracle中区分大小写的重复值

时间:2017-12-05 10:19:22

标签: sql oracle duplicates

我有一张包含以下样本记录的表

Name    Profession      University
-----   --------------  ---------------
Ram     Student         xxxUniversity
Ravi    Professor       xyzUniversity
Reshma  Professor       abcUniversity
Ram     StudenT         xxxUniversity

如上所述" Ram"具有区分大小写的重复记录我的Oracle查询应该为" Ram"返回不同的记录和一条记录,它在Profession列中具有最大的大写字符数。

预期结果

    Name    Profession      University
    -----   --------------  ---------------
    Ravi    Professor       xyzUniversity
    Reshma  Professor       abcUniversity
    Ram     StudenT         xxxUniversity

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT name,
       Profession,
       university
FROM
(SELECT name,
       Profession,
       university,
       ROW_NUMBER() OVER (PARTITION BY NAME,UPPER(PROFESSION) ORDER BY REGEXP_COUNT (Profession, '[A-Z]') DESC) RN
  FROM table1)
WHERE RN=1;

答案 1 :(得分:1)

获取分配在每个列的唯一值上的行号转换为大写(或更低)的大小写:

SELECT name, profession, university
FROM   (
  SELECT name, profession, university,
         ROW_NUMBER()
           OVER (
             PARTITION BY
               UPPER( name ),
               UPPER( profession ),
               UPPER( university )
             ORDER BY
               LENGTH( TRANSLATE( profession, ' abcdefghijklmnopqrstuvwxyz', ' ' ) ) DESC
           )
           AS rn
  FROM   your_table
)
WHERE  rn = 1;