简体   繁体   中英

How do i get only the max value of a column SQL

i'm using this code

SELECT S.id_usuario, C.cnt
  FROM promos_usuarios S
       INNER JOIN (SELECT id_usuario, count(id_usuario) as cnt
                     FROM promos_usuarios 
                    GROUP BY id_usuario) C ON S.id_usuario = c.id_usuario

after using this code, i get this table

And i only need the max value of the column cnt, not the entire count, what can i do to make this work? i'm sorry if this doesnt make any sense, english isnt my main lenguage.

You can use a correlated subquery:

SELECT id_usuario, COUNT(*) as cnt
FROM promos_usuarios 
GROUP BY id_usuario
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROW ONLY;

Note all databases support FETCH FIRST . Your database may use SELECT TOP or LIMIT or something else.

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