Bhushan
Bhushan

Reputation: 3

Sql Query t find out maximum from name field

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

Answers (2)

Pero P.
Pero P.

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

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

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

Upvotes: 6

Related Questions