简体   繁体   中英

How to find the maximum value among a range of count

it is the first time that I ask a question about stackoverflow and I have recently used MySQL.
I need a query that allows me to find the one with the largest value from a range of counts.
I will give you an example showing a "pseudo code" obviously not working:

/* function TicketCount(:time){ */
SELECT COUNT(`idT`)
FROM `TICKET`
WHERE `startTime`<=(TIME(':time')+INTERVAL 10 MINUTE) AND `startTime`>(TIME(':time')-INTERVAL 35 MINUTE);
/* } */

SELECT MAX( /* TicketCount( from (TIME(':time')-INTERVAL 35 MINUTE) to (TIME(':time')+INTERVAL 10 MINUTE) ) */ );



To give you an example, my table, at the moment, is this:

 +-----+-----------+ | idT | startTime | +-----+-----------+ | 10 | 13:00:00 | | 9 | 12:30:00 | | 8 | 12:30:00 | | 7 | 11:50:00 | | 6 | 11:30:00 | | 11 | 13:00:00 | +-----+-----------+

If ":val" were '13:00:00', the result must be '4'.
If ":val" were '11:00:00', the result must be '2'.
If ":val" were '13:35:00', the result must be '0'.


I hope it has made me understand and I hope there is a solution that does not consult other programming languages... Thanks in advance!

Consider the following, but note that this doesn't account for ties...

SELECT :val1
     , COUNT(*) total 
  FROM TICKET 
 WHERE startTime <= (TIME(':val')+INTERVAL 10 MINUTE) 
   AND startTime >  (TIME(':val')-INTERVAL 35 MINUTE)
 UNION
SELECT :val2
     , COUNT(*)  
  FROM TICKET 
 WHERE startTime <= (TIME(':val')+INTERVAL 10 MINUTE) 
   AND startTime >  (TIME(':val')-INTERVAL 35 MINUTE)
SELECT :val3
     , COUNT(*)  
  FROM TICKET 
 WHERE startTime <= (TIME(':val')+INTERVAL 10 MINUTE) 
   AND startTime >  (TIME(':val')-INTERVAL 35 MINUTE)
 ORDER  
    BY total DESC 
 LIMIT 1;
SELECT SUM(startTime BETWEEN ADDTIME(@time, '-00:35:00') AND ADDTIME(@time, '00:10:00'))
FROM ticket;

fiddle

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