开发者

Sql Query t find out maximum from name field

开发者 https://www.devze.com 2023-01-25 02:25 出处:网络
I have 5 tables with field name name 开发者_如何学编程. &which name is appear maximum time in each table then I need to find out which output is maximum in all answerselect top 1 name, count(*) fr

I have 5 tables with field name name 开发者_如何学编程. & which name is appear maximum time in each table then I need to find out which output is maximum in all answer


select top 1 name, count(*) from table
group by name
order by count(*) desc


Just to add to the other response, if there is more than one name with the top number of occurrences then something along these lines will work:

;WITH cte (Name, NoOccurrences) AS 
(
  SELECT Name, COUNT(Name) FROM [Table] GROUP BY Name
)
SELECT Name, NoOccurrences FROM cte 
WHERE NoOccurrences = (SELECT MAX(NoOccurrences) FROM cte) 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号