Reputation: 3
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
Upvotes: 0
Views: 197
Reputation: 26992
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)
Upvotes: 1
Reputation: 55489
select top 1 name, count(*) from table
group by name
order by count(*) desc
Upvotes: 6