简体   繁体   English

SQL查询在发生日期获取最大值

[英]SQL Query Get Max Value At Occured Date

So we are doing some traffic reporting in our department. 因此,我们正在部门中进行一些流量报告。 Therefore we got a table named traffic_report, which is build up like 因此,我们得到了一个名为traffic_report的表,该表的构建类似于

╔════════════════╦═══════════╦═════════════════════╦═════════════╦═════════════╗
║    hostname    ║ interface ║      date_gmt       ║ intraf_mpbs ║ outraf_mbps ║
╠════════════════╬═══════════╬═════════════════════╬═════════════╬═════════════╣
║ my-machine.com ║ NIC-5     ║ 2013-09-18 09:55:00 ║          32 ║          22 ║
║ my-machine.com ║ NIC-5     ║ 2013-09-17 08:25:00 ║          55 ║          72 ║
║ my-machine.com ║ NIC-5     ║ 2013-09-16 05:12:00 ║          65 ║           2 ║
║ my-machine.com ║ NIC-5     ║ 2013-09-15 04:46:00 ║          43 ║           5 ║
║ my-machine.com ║ NIC-5     ║ 2013-09-14 12:02:00 ║          22 ║          21 ║
║ my-machine.com ║ NIC-5     ║ 2013-09-13 22:13:00 ║          66 ║          64 ║
╚════════════════╩═══════════╩═════════════════════╩═════════════╩═════════════╝

I'd like to fetch the maximum of the traffic in and traffic out at the occured date. 我想获取发生日期的最大流量进出流量。 My approach doing so is like this 我这样做的方法是这样的

SELECT hostname, interface, date_gmt, max(intraf_mbps) as max_in, max(outtraf_mbps) as max_out
FROM traffic_report
GROUP by hostname, interface

The approach produces a table like this 该方法产生这样的表

╔════════════════╦════════════╦═════════════════════╦════════╦═════════╗
║    hostname    ║ interface  ║      date_gmt       ║ max_in ║ max_out ║
╠════════════════╬════════════╬═════════════════════╬════════╬═════════╣
║ my-machine.com ║ NIC-5      ║ 2013-09-18 09:55:00 ║     66 ║      72 ║
╚════════════════╩════════════╩═════════════════════╩════════╩═════════╝

The problem is, the date_gmt displayed is just the date of the first record entered to the table. 问题是,显示的date_gmt只是输入到表中的第一条记录的日期。

How do I instruct SQL to display me the date_gmt at which the max(intraf_mbps) occured? 如何指示SQL向我显示发生max(intraf_mbps)的date_gmt?

Your issue is with mysql hidden fields : 您的问题与mysql隐藏字段有关

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. MySQL扩展了GROUP BY的使用,以便选择列表可以引用未在GROUP BY子句中命名的非聚合列。 This means that the preceding query is legal in MySQL. 这意味着前面的查询在MySQL中是合法的。 You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. 您可以使用此功能来避免不必要的列排序和分组,从而获得更好的性能。 However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. 但是, 这主要在每个未聚合列中未在GROUP BY中命名的所有值对于每个组都相同时才有用。

Mysql has not rank features either analytic functions, to get your results, a readable approach but with very poor performance is: Mysql既没有排名功能,也没有解析功能,为了获得您的结果,这是一种可读的方法,但是性能很差,它是:

SELECT hostname, 
       interface, 
       date_gmt, 
       intraf_mbps, 
       outtraf_mbps
FROM traffic_report T
where intraf_mbps + outtraf_mbps =
      ( select 
           max(intraf_mbps + outtraf_mbps) 
        FROM traffic_report T2
        WHERE T2.hostname = T.hostname and
              T2.interface = T.interface 
        GROUP by hostname, interface
      )

Sure you can work for a solution with more index friendly approach or avoid correlated subquery. 确保您可以使用索引更友好的方法来解决方案,或者避免使用相关的子查询。

Notice than I have added both rates, in and out. 请注意,然后我将进,出两种价格都添加了。 Adapt solution to your needs. 根据您的需求调整解决方案。

Either of these approaches should work: 这些方法中的任何一种都应该起作用:

This first query returns the rows that match the maximum out and in values, so multiple rows can be returned if many records share the max or min values. 第一个查询返回与最大输出和最大输入值匹配的行,因此,如果许多记录共享最大或最小值,则可以返回多个行。

SELECT * from traffic_report 
WHERE intraf_mpbs = (SELECT MAX(intraf_mpbs) FROM traffic_report) 
   or outraf_mpbs = (SELECT MAX(outraf_mpbs) FROM traffic_report)

This second query returns more of a MI style result, add other fields if you require them. 第二个查询返回更多的MI风格结果,如果需要,请添加其他字段。

SELECT "MAX IN TRAFFIC" AS stat_label,date_gmt AS stat_date, traffic_report.intraf_mpbs
  FROM traffic_report,(select MAX(intraf_mpbs) AS max_traf FROM traffic_report) as max_in
 WHERE traffic_report.intraf_mpbs = max_in.max_traf
 UNION
SELECT "MAX OUT TRAFFIC" AS stat_label,date_gmt AS stat_date, traffic_report.outraf_mpbs
  FROM traffic_report,(SELECT MAX(outraf_mpbs) AS max_traf FROM traffic_report) AS max_out
 WHERE traffic_report.outraf_mpbs = max_out.max_traf

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM