简体   繁体   English

如何选择具有MAX(datetime)的行并获取列的最小值?

[英]How do I select rows with MAX(datetime) and also get min values for a column?

My table is like this (np_capacity): 我的表是这样的(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

I want to select all the tower with the max date then for those towers I also want the min capacity value in the table. 我想选择所有具有最大日期的塔,然后为这些塔选择表中的最小容量值。

Result would be: 结果将是:

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

What I have is this but it doesnt always give my the correct min values. 我所拥有的是这个,但它并不总是能给出正确的最小值。

SELECT npc.*, groupedcap.MinCapacity
FROM np_capacity npc
INNER JOIN
(SELECT tower, MAX(date) AS MaxDate
FROM np_capacity
GROUP BY tower) groupednpc 
ON npc.tower = groupednpc.tower 
INNER JOIN 
(SELECT tower, MIN(capacity) AS MinCapacity
FROM np_capacity
GROUP BY tower) groupedcap 
ON npc.tower = groupedcap.tower 
AND npc.date = groupednpc.MaxDate

You can use a subselect to calculate the min capacity, and the max date. 您可以使用子选择来计算最小容量和最大日期。 Then, join with the table to get other fields. 然后,与表联接以获取其他字段。

select npc.*, calc.minCapacity
from (
    select tower, max(datetime) maxDate, min(capacity) minCapacity
    from np_capacity
    group by tower
) calc
join np_capacity npc on (npc.tower = calc.tower
                         and npc.datetime = npc.maxDate)

This request select all towers, and for each the maxdatetime and mincapacity. 该请求选择所有塔,并选择每个塔的最大日期时间和最小容量。

If you want just the towers with the maxdatetime, you can use : 如果只需要具有maxdatetime的塔,则可以使用:

select npc.*, (select min(c2.capacity) from np_capacity c2 
               where c2.tower = npc.tower) minCapacity
from (select max(datetime) maxDatetime from np_capacity) c1
join np_capacity npc on (npc.datetime = c1.maxDatetime)
SELECT `a`.`id`,`a`.`tower`,`a`.`datetime`,`a`.`capacity`,(SELECT MIN(`b`.`capacity`) 
FROM `np_capacity` `b` WHERE `b`.`tower`=`a`.`tower` ) AS `MinCapacity` FROM `np_capacity` `a` 
WHERE `a`.`datetime`=(SELECT MAX(`datetime`) FROM `np_capacity`);

Test the above query on SQL Fiddle SQL Fiddle上测试以上查询

Another answer is by using order by and limit 1 in sub query : 另一个答案是在子查询中使用order by和limit 1:

select t1.*,(select capacity from np_capacity t3 
  where t1.tower=t3.tower 
  order by capacity limit 1) mincapacity  
  from np_capacity t1 where  
 datetime=(select datetime from np_capacity
  order by datetime desc limit 1);

Test it on sqlfiddle sqlfiddle测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择具有MAX(datetime)的行,并且还获得带有列日期的min值? - Select rows with MAX(datetime) and also get min values with their dates for a column? 如何获取SQL中具有MAX和MIN值的行的ID - How do I get the ID of a rows which have MAX and MIN values in SQL 如何使用自联接选择最小值和最大值 - How do I select the min and max values using a self join 如何获取SQL中一组行的最小值和最大值? - How do I get the min and max of a set of rows in SQL? 如何在最大日期时间(另一个列)的基础上选择不同的列,还要在MySQL中连接两个表? - How can I select distinct column based on max datetime (another column) also joining two tables in MySQL? MySQL获得选择行的最大值和最小值之间的差异 - MySQL get difference between max and min values for select rows 如何通过另一个MAX(datetime)列选择列? - How can I select column via another MAX(datetime) column? 如何在 SQL 中将 SELECT 行与 MIN(DateTime 列)、另一列分组和另一列 DISTINCT? - How to SELECT rows with MIN(DateTime column), GROUP by another column and DISTINCT by another column in SQL? Select MIN, MAX 对应列基于另一列值 - Select MIN, MAX Corresponding column based on another column values 如何在SQL上使用mindiff(),max()和datediff来工作? - How do i get datediff to work with min() max() on sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM