简体   繁体   中英

SQL Query MAX(count(*))

I need to get IdPasajero and the maximum number of tickets purchased from the list of a Boletos table that has the record of all tickets purchased by IdPasajero .

any ideas?

My Thoughts:

SELECT MAX(contador) FROM (
    SELECT IdPasajero, count(idPasajero) contador 
    FROM Boleto
    GROUP BY IdPasajero
) T;

This returns only the maximum amount purchased by a passenger or "IdPasajero"

Use ORDER BY and TOP :

SELECT TOP (1) IdPasajero, count(idPasajero) as contador
FROM Boleto
GROUP BY IdPasajero
ORDER BY contador DESC;

If you want all values in the case of ties, use TOP (1) WITH TIES .

try this :

with NbID as (
SELECT IdPasajero, count(IdPasajero) as Nb 
FROM Boleto
group by IdPasajero
),

Maxi as (
select max(Nb) from MaxiID
)
select NbID.* from NbID inner join Maxi on NbID.Nb=Maxi.MaxiID

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