简体   繁体   中英

Get only First Row or Highest Value SQL

I am trying to get the first row (also highest value of this query) it is now posting everything

When I try adding Limit/NumRow to just get the first row (also highest value) for example it causes an error in the query. The QUERY is

select
  title
, min(year)
, max(year)
, count(appointment) AS num_appointments 
from appointments 
group by title 
ORDER BY title 
ORDER BY count(title) desc;

You can try the below - limit will work for MySql/Postgres

select title, min(year), max(year), 
count(appointment) AS num_appointments 
from appointments group by title 
ORDER BY count(appointment) desc
limit 1

If your database is SQL Server - you can use TOP like below -

select TOP 1 title, min(year), max(year), 
    count(appointment) AS num_appointments 
    from appointments group by title 
    ORDER BY count(appointment) desc
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM