MYSQL:获取最后第二个最大值行,但某些日期未显示。为什么?
使用更大查询的此查询小节:MYSQL:获取最后第二个最大值行,但某些日期未显示。为什么?,mysql,Mysql,使用更大查询的此查询小节: -- Get second last transaction per item select max(i.transaction), item from ( -- Rows not including max transaction select item, transaction from positivetest.history_items where transaction not in ( -- Max tran
-- Get second last transaction per item
select max(i.transaction), item
from (
-- Rows not including max transaction
select item, transaction
from positivetest.history_items
where transaction not in (
-- Max transaction per item
select max(transaction)
from positivetest.history_items
group by item
)
) i
group by item
及以下有关资料。为了简单起见,我正在修剪真实数据库中的数据,例如,项0实际上有16个条目:
交易
项目
294474
0
294474
0
715439
0
1299009
0
119427
1.
137450
1.
177632
3.
325260
3.
669580
3.
871509
3.
951363
3.
539784
4.
845808
4.
468110
5.
965661
5.
1326575
5.
1327567
5.
您可以使用行号,如中所示:
select *
from (
select *,
row_number() over(partition by item order by transaction desc) as rn
from positivetest.history_items
) x
where rn = 2 -- filtering the second one, using descending order