sql - Select records with maximum value -
i have table called: customers
.
i'm trying name , salary of people have maximum salary.
so have tried this:
select name, salary maxsalary customers group salary having salary = max(salary)
unfortunately, got error:
column 'customers.name' invalid in select list because not contained in either aggregate function or group clause.
i know should add name column group by
clause, records of table.
i know can by:
select name, salary customers salary = (select max(salary) customers)
but want achieve group by
, having
clauses.
this requirement isn't suited group by
, having
solution. easiest way so, assuming you're using modern-insh version of ms sql server, use rank
window function:
select name, salary (select name, salary, rank() on (order salary desc) rk customers) c rk = 1
Comments
Post a Comment