SQL Query To Obtain Value that Occurs more than once

前端 未结 7 1987
陌清茗
陌清茗 2020-12-08 19:26

I need to query my database to show the records inside my table where lastname occurs more than three times. Example: in my Students Table, there are 3 people with Lastname

相关标签:
7条回答
  • 2020-12-08 19:51

    For SQL Server 2005+

    ;WITH T AS
    (
    SELECT *, 
           COUNT(*) OVER (PARTITION BY Lastname) as Cnt
    FROM Students
    )
    SELECT * /*TODO: Add column list. Don't use "*"                   */
    FROM T
    WHERE Cnt >= 3
    
    0 讨论(0)
提交回复
热议问题