0

我需要查询 MySQL 5.5 的帮助

在按 location_id、count_number、try_number 对行进行排序后,我试图只为 2 列(tan、location_id)的组合选择 1 行。所以基本上我想要一个只返回下图中黄色行的查询。每个 tan/location_id 组合只有 1 条记录,该组中“count_number”和“try_number”最高。

在此处输入图像描述

这是我目前的查询。

select tan, quantity, count_number, try_number, location_id 
from inventario_inventoryregistry
where tan = '53-100554-01'
order by location_id desc, count_number desc, try_number desc;
4

2 回答 2

1

我认为这可以满足您的要求:

select iir.*
from inventario_inventoryregistry iir
where (count_number, try_number) = (select iir2.count_number, iir2.try_number
                                    from inventario_inventoryregistry iir2
                                    where iir2.tan = iir.tan and iir2.location_id = iir.location_id
                                    order by iir2.count_number desc, iir2.try_number desc
                                    limit 1
                                   );
于 2018-06-22T02:10:15.053 回答
0
select tan, quantity, count_number, try_number, location_id
from
(select tan, quantity, count_number, try_number, location_id, row_number() over (partition by location_id, tan_id order by location_id desc, count_number desc, try_number desc) rw_nm
from inventario_inventoryregistry
where tan = '53-100554-01')
where rw_nm = 1;
于 2018-06-22T02:20:28.773 回答