【问题标题】:SQL - Find rows starting with the same charactersSQL - 查找以相同字符开头的行
【发布时间】:2014-05-21 02:14:56
【问题描述】:

我有一个 oracle 数据库,其中包含可能以相同前缀开头的数据的表,并且想查找表中某处重复 5 位前缀的行。 例如:

Table1 
---------------
12345-brsd
12345-wbgb
12345-ydad
34573-diwe
75234-daie
72456-woei
72456-wdgq

我只想返回前 5 位重复的数字,所以在这个示例中:

12345-brsd
12345-wbgb
12345-ydad
72456-woei
72456-wdgq

【问题讨论】:

  • 使用GROUP BY 将具有相同前缀的行组合在一起,并为每行提取一行,并使用HAVING count(*) > 1 来查找骗子。

标签: sql database oracle


【解决方案1】:

您可以使用分析函数来做到这一点:

select t.*
from (select t.*, count(*) over (partition by substr(column, 1, 5)) as cnt
      from table t
     ) t
where cnt > 1
order by column1;

【讨论】:

    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多