SQL क्वेरी, गिनती और समूह द्वारा

original title: "SQL query, count and group by"


Translate

If I have data like this:

+---+----+
|Key|Name|
+---+----+
|1  |Dan |
+---+----+
|2  |Tom |
+---+----+
|3  |Jon |
+---+----+
|4  |Tom |
+---+----+
|5  |Sam |
+---+----+
|6  |Dan |
+---+----+

What is the SQL query to bring back the records where Name is repeated 2 or more times?

So the result I would want is

 +---+
 |Tom|
 +---+
 |Dan|
 +---+


अगर मेरे पास इस तरह का डेटा है: + --- + ---- + | कुंजी | नाम | + --- + ---- + | 1 | दान | + --- + ---- + | 2 | टॉम | + --- + ---- + | 3 | जॉन | + --- + ---- + | 4 | टॉम | + --- + ---- + | 5 | सैम | + --- + ---- + | 6 | दान | + --- + ---- + जो ...

यह अनुवाद के बाद का सारांश है, अगर आपको पूरा अनुवाद देखने की आवश्यकता है, तो कृपया 'अनुवाद' आइकन पर क्लिक करें


सभी उत्तर
  • Translate

    Couldn't be simpler...

    Select
    Name,
    Count(Name) As Count
    From
    Table
    Group By
    Name
    Having
    Count(Name) > 1
    Order By
    Count(Name) Desc
    

    This could also be extended to delete duplicates:

    Delete
    From
    Table
    Where
    Key In (
        Select
        Max(Key)
        From
        Table
        Group By
        Name
        Having
    Count(Name) > 1)
    

  • Translate
    select name from table group by name having count(name) > 1
    

  • Translate

    This could also be accomplished by joining the table with itself,

    SELECT DISTINCT t1.name
    FROM    tbl t1
            INNER JOIN tbl t2
            ON      t1.name = t2.name
    WHERE   t1.key         != t2.key;