sql query to find the duplicate records

后端 未结 5 1135
谎友^
谎友^ 2020-12-31 07:24

what is the sql query to find the duplicate records and display in descending, based on the highest count and the id display the records.

for example:

gettin

相关标签:
5条回答
  • 2020-12-31 07:59
    select distinct title, (
                   select count(title) 
                   from kmovies as sub 
                   where sub.title=kmovies.title) as cnt 
    from kmovies 
    group by title 
    order by cnt desc
    
    0 讨论(0)
  • 2020-12-31 08:01

    You can do it in a single query:

    Select t.Id, t.title, z.dupCount
    From yourtable T
    Join
       (select title, Count (*) dupCount
        from yourtable 
        group By title
        Having Count(*) > 1) z
       On z.title = t.Title
    order By dupCount Desc
    
    0 讨论(0)
  • 2020-12-31 08:03

    If your RDBMS supports the OVER clause...

    SELECT
       title
    FROM
        (
        select
           title, count(*) OVER (PARTITION BY title) as cnt
        from
          kmovies
        ) T
    ORDER BY
       cnt DESC
    
    0 讨论(0)
  • 2020-12-31 08:04

    This query uses the Group By and and Having clauses to allow you to select (locate and list out) for each duplicate record. The As clause is a convenience to refer to Quantity in the select and Order By clauses, but is not really part of getting you the duplicate rows.

    Select
        Title,
        Count( Title ) As [Quantity]
       From
        Training
       Group By
        Title
       Having 
        Count( Title ) > 1
       Order By
        Quantity desc
    
    0 讨论(0)
  • 2020-12-31 08:05

    You can't do it as a simple single query, but this would do:

    select title
    from kmovies
    where title in (
        select title
        from kmovies
        group by title
        order by cnt desc
        having count(title) > 1
    )
    
    0 讨论(0)
提交回复
热议问题