SQL: Select rows with a column value that occurs at least N times?

前端 未结 3 1001
情深已故
情深已故 2020-12-04 22:27

Suppose I have a SQL table \"Celebrities\" with two columns: \"fname\" and \"lname\":

fname    | lname    
---------+-------  
Bill     | Clinton
Bill     |          


        
3条回答
  •  Happy的楠姐
    2020-12-04 22:53

    select fname, lname
    from 
      (
        select fname, lname, count(*) over(partition by lname) as lcount
        from Celebrities
      ) as S
    where lcount > 1
    

    Tested in SQL Server 2008. Might work in other DBMS that support count(*) over(...)

提交回复
热议问题