简体   繁体   中英

Select rows with MAX(datetime) and also get min values with their dates for a column?

My table is like this (np_capacity):

id  tower  datetime             capacity
---|----|---------------------|----------
1  | A  | 2016-05-29 09:02:41 | 34676
2  | B  | 2016-05-29 09:02:41 | 10736
5  | C  | 2016-05-29 09:02:41 | 55664
3  | D  | 2016-05-29 09:02:41 | 32622
4  | A  | 2016-05-29 13:08:38 | 5474
6  | B  | 2016-05-29 13:08:38 | 20692
7  | C  | 2016-05-29 13:08:38 | 134802
8  | D  | 2016-05-29 13:08:38 | 4754

and this is my query:

SELECT npc.*,
                (SELECT min(c2.capacity) FROM np_capacity c2 WHERE c2.tower = npc.tower AND datetime BETWEEN (NOW() - INTERVAL 14 DAY) AND NOW()) minCapacity,

                FROM (SELECT max(datetime) maxDatetime FROM np_capacity) c1
                JOIN np_capacity npc ON (npc.datetime = c1.maxDatetime)

It will select all the towers with the max datetime then for those towers it will also select the min capacity value in the table for the past two weeks. The output is something like this:

id  tower  datetime             capacity   MinCapacity
---|----|---------------------|----------|-------------
4  | A  | 2016-05-29 13:08:38 | 5474     | 5474
6  | B  | 2016-05-29 13:08:38 | 20692    | 10736
7  | C  | 2016-05-29 13:08:38 | 134802   | 55664
8  | D  | 2016-05-29 13:08:38 | 4754     | 4754

Everything good so far. The datetime is for the max(datetime) and the MinCapacity is the min value for the past two weeks and also what I want. How can I get the specific datetime also for selected MinCapacity. (MinCapacityDate)

You can try this, demonstrated in SQLFiddle . If there is more than 1 row with the minimum capacity, it returns the most recent date.

SELECT np_capacity.id, np_capacity.tower, np_capacity.datetime
        ,np_capacity.capacity, mincap.mincapacity
         ,  MAX(tmincapdatetime.datetime) AS datetimeOfMin
FROM (SELECT MAX(`datetime`) AS maxdatetime 
      FROM np_capacity) AS tmaxdatetime
JOIN np_capacity
  ON tmaxdatetime.maxdatetime = np_capacity.`datetime`
LEFT JOIN (SELECT tower, MIN(capacity) as mincapacity
           FROM np_capacity
           WHERE `datetime` BETWEEN (NOW() - INTERVAL 14 DAY) AND NOW()
           GROUP BY tower) AS mincap
  ON mincap.tower = np_capacity.tower  
LEFT JOIN np_capacity AS tmincapdatetime
   ON tmincapdatetime.`datetime` BETWEEN (NOW() - INTERVAL 14 DAY) AND NOW()
      AND mincap.tower = tmincapdatetime.tower
      AND mincap.mincapacity = tmincapdatetime.capacity
GROUP BY np_capacity.id, np_capacity.tower, np_capacity.datetime
  ,np_capacity.capacity, mincap.mincapacity
ORDER BY tower;

Could by you need a join and a where in

select a.*, b.min(capacity) 
from np_capacity as a
where a.tower, datetime in (      
    select tower, max(datetime) 
    from np_capacity 
    group by tower) 
inner join np_capacity as b on a.tower  = b.tower 
group by a.id, a.tower, a.datetime 

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