gpt4 book ai didi

mysql - SQL 条件聚合函数

转载 作者:行者123 更新时间:2023-11-29 22:15:29 24 4
gpt4 key购买 nike

我有以下表结构:

CREATE TABLE `divisions` (
`ID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`ID`));

CREATE TABLE `div_postcodes` (
`div_id` INT NOT NULL,
`postcode` INT NOT NULL,
`percentage` DECIMAL NULL,
PRIMARY KEY (`div_id`, `postcode`));

该表存储有关邮政编码的信息,以及它们属于哪个选区,以及该邮政编码区域属于该选区的百分比。

我需要获取的是每个选区的邮政编码列表,但我只希望它出现在列表中,如果它具有最高的百分比,因此邮政编码应该只出现一次。如果可以使用 group_concat 将邮政编码连接到字符串中,那就太好了。

例如:邮政编码如下:

div_id, postcode, percentage
601, 828, 90.10
602, 828, 9.90

我只想将邮政编码 828 列在 div_id 601 下,因为它覆盖了 90%

http://www.sqlfiddle.com/#!9/90997

最佳答案

以下查询将为您提供分区和邮政编码的单一组合 - 百分比最高的组合。这是您要找的吗?

select div_id, postcode, max(percentage)
from div_postcodes
group by div_id, postcode

关于mysql - SQL 条件聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com