MySQL,分组依据和最新结果
SELECT
o.semaine AS week,
c.id AS c_id,
sr.id AS sr_id,
mr.id AS mr_id,
o.`%_RACH_distance_sup_5075m` AS rach,
o.DCR_Total_AMR AS dcr_amr
FROM
rdi.ref_cells AS c
INNER JOIN nortel.OM_Cell_week AS o ON o.cellule = c.libelle
INNER JOIN rdi.ref_sites AS sr ON sr.id = c.siteRadio_id
INNER JOIN rdi.ref_mres AS mr ON o.rnc = mr.libelle AND sr.milieuReseau_id = mr.id
INNER JOIN rdi.ref_pl AS p ON c.plaque_id = p.id
WHERE
o.date > ADDDATE(NOW(), INTERVAL - 3 WEEK) AND
o.`%_RACH_distance_sup_5075m` > 50 AND
o.DCR_Total_AMR > 1.5
结果:
+------+--------+-------+-------+---------+---------+
| week | c_id | sr_id | mr_id | rach | dcr_amr |
+------+--------+-------+-------+---------+---------+
| 16 | 117114 | 37312 | 79 | 64,1198 | 1,5018 |
| 17 | 117114 | 37312 | 79 | 67,6647 | 1,79469 |
| 18 | 117114 | 37312 | 79 | 66,6645 | 1,51302 | <- this
| 16 | 117116 | 37312 | 50 | 69,1325 | 2,3014 |
| 17 | 117116 | 37312 | 50 | 67,6647 | 1,568 | <- this
+------+--------+-------+-------+---------+---------+
我想选择本周最高的最新结果(第 3 行和第 5 行)。 我尝试添加 GROUP BY c.id
但默认情况下它返回每个组的第一个。 我还尝试了 ORDER BY o.semaine
SELECT
o.semaine AS week,
c.id AS c_id,
sr.id AS sr_id,
mr.id AS mr_id,
o.`%_RACH_distance_sup_5075m` AS rach,
o.DCR_Total_AMR AS dcr_amr
FROM
rdi.ref_cells AS c
INNER JOIN nortel.OM_Cell_week AS o ON o.cellule = c.libelle
INNER JOIN rdi.ref_sites AS sr ON sr.id = c.siteRadio_id
INNER JOIN rdi.ref_mres AS mr ON o.rnc = mr.libelle AND sr.milieuReseau_id = mr.id
INNER JOIN rdi.ref_pl AS p ON c.plaque_id = p.id
WHERE
o.date > ADDDATE(NOW(), INTERVAL - 3 WEEK) AND
o.`%_RACH_distance_sup_5075m` > 50 AND
o.DCR_Total_AMR > 1.5
The result:
+------+--------+-------+-------+---------+---------+
| week | c_id | sr_id | mr_id | rach | dcr_amr |
+------+--------+-------+-------+---------+---------+
| 16 | 117114 | 37312 | 79 | 64,1198 | 1,5018 |
| 17 | 117114 | 37312 | 79 | 67,6647 | 1,79469 |
| 18 | 117114 | 37312 | 79 | 66,6645 | 1,51302 | <- this
| 16 | 117116 | 37312 | 50 | 69,1325 | 2,3014 |
| 17 | 117116 | 37312 | 50 | 67,6647 | 1,568 | <- this
+------+--------+-------+-------+---------+---------+
I'd like to select the latest results (line 3 and 5) where the week is the highest.
I tried to add GROUP BY c.id
but by default it returns the first of each group.
I also tried ORDER BY o.semaine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指出的两条线不是本周最高的两条线,所以我不确定您的意思。
无论如何,您始终可以使用子查询来执行此类操作。以下是选择周数最高的行的示例:
select * from table where week = (select max(week) from table)
The two lines you pointed are not the ones where the week is the highest, so i'm not sure what you mean.
Anyway, you can always use sub-queries to perform such things. Here is a sample for selecting the rows with the highest week:
select * from table where week = (select max(week) from table)