简体   繁体   English

聚合时的MySQL语法查询

[英]MySQL syntax query on aggregation

I have a table called TICKET with columns FLIGHTNUMBER and INSURED. 我有一个名为TICKET的表,其中包含FLIGHTNUMBER和INSURED列。 INSURED has only 0 or 1 values. INSURED只有0或1个值。 FLIGHTNUMBER has duplicates. FLIGHTNUMBER有重复项。 I wanted to get the FLIGHTNUMBER with the max number of 1s in INSURED. 我想获取INSURED中最大数量为1的FLIGHTNUMBER。 Here's what I tried: 这是我尝试过的:

SELECT FLIGHTNUMBER 
FROM (SELECT FLIGHTNUMBER,SUM(INSURED) AS INSURANCE_COUNT 
      FROM TICKET 
      GROUP BY TICKET.FLIGHTNUMBER) AS OUT2 
WHERE OUT2.INSURANCE_COUNT = MAX(INSURANCE_COUNT) ;

AND

SELECT FLIGHTNUMBER 
FROM (SELECT FLIGHTNUMBER,SUM(INSURED) AS INSURANCE_COUNT 
      FROM TICKET 
      GROUP BY TICKET.FLIGHTNUMBER 
      HAVING INSURANCE_COUNT = MAX(SUM(INSURED))) AS OUT2;

But both are causing errors. 但是两者都导致错误。 Could you please tell me the correct syntax. 您能告诉我正确的语法吗? Thanks in advance. 提前致谢。

The first one doesn't work because you can't use an aggregation function in a WHERE clause. 第一个不起作用,因为您不能在WHERE子句中使用聚合函数。 Aggregation doesn't happen until after all the rows are selected, and WHERE is used to select the rows, so there's a chicken-and-egg problem. 只有在选择了所有行之后才进行聚合,并且使用WHERE来选择行,因此存在鸡与蛋的问题。

The second doesn't work because you can't nest aggregation functions. 第二个不起作用,因为您不能嵌套聚合函数。

You don't need to calculate MAX(INSURANCE_COUNT) . 您不需要计算MAX(INSURANCE_COUNT) Just order the query and take the first one. 只需订购查询并进行第一个查询。

SELECT flightnumber
FROM (SELECT flightnumber, SUM(INSURED) AS insurance_count
      FROM ticket
      GROUP BY flightnumber) AS out2
ORDER BY insurance_count DESC
LIMIT 1

You don't even need insurance_count in the SELECT list, so you don't need to wrap it in a subquery to get rid of it. 您甚至在SELECT列表中甚至不需要insurance_count ,因此您无需将其包装在子查询中即可摆脱它。

SELECT flightnumber
FROM ticket
GROUP BY flightnumber
ORDER BY SUM(insured) DESC
LIMIT 1

However, if there are multiple flights that are tied for the max, this will just return one of them. 但是,如果有多个航班并列为最高限额,这只会返回其中之一。 If you need to get all of them, you need to write a subquery that gets the max, and join with that. 如果需要全部获取,则需要编写一个获取最大值的子查询,并与之联接。

SELECT flightnumber
FROM (SELECT flightnumber, SUM(insured) AS insurance_count
      FROM ticket
      GROUP BY flightnumber) AS out2
JOIN (SELECT SUM(insured) AS max_insurance_count
      FROM ticket
      GROUP BY flightnumber
      ORDER BY max_insurance_count DESC
      LIMIT 1) AS out3
ON out2.insurance_count = out3.max_insurance_count

You can do: 你可以做:

SELECT flightnumber, SUM(insured) AS insured_count 
FROM ticket 
GROUP BY flightnumber 
ORDER BY insured_count DESC 
LIMIT 1;

This is just a SUM aggregation on the insured column, grouped by flightnumber , and sorted on the insured_count in descending order. 这只是insured列上的SUM聚合,按flightnumber分组,并按降序在insured_count上排序。 We only keep the first result. 我们只保留第一个结果。

This doesn't consider what would happen if two flightnumbers had the same max insured count. 这没有考虑如果两个航班号具有相同的最大保险人数会发生什么。 You could break ties with a second ORDER BY field, or run another outer query. 您可以使用第二个ORDER BY字段打破关系,或者运行另一个外部查询。

If you had a flightnumber table you could just update its insured_count field with a SUM() every time you modify the ticket table. 如果您有flightnumber表,则每次修改ticket表时都可以使用SUM()更新其insured_count字段。 That would be more efficient if you're running this report very often, the ticket table ends up with a lot of rows, and most of the rows aren't changing after a flight is complete. 如果您经常运行此报告, ticket表以很多行结尾,并且大多数行在完成飞行后都没有更改,那会更有效率。

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

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