You can use Mysql's user defined variables and give a rank to rows per customer and per inventory with in a same customer group,below query will give 2 latest int_proc
per inventory and same customer group if you want to get latest n number of records just change where clause to where t2.r <= n
select
t2.id,
t2.customer_id,
t2.int_proc,
t2.inventory
from (
select t.*,
@r:= case when @g = t.customer_id
then
case when @sg = t.inventory
then @r+1
else 1 end
else 1 end r,
@g:= t.customer_id g,
@sg:= t.inventory sg
from test t
cross join (select @g:=null,@sg:=null,@r:=null) t1
where t.customer_id in('A','B')
order by t.customer_id,t.inventory,t.int_proc desc
) t2
where t2.r <= 2
order by id
Fiddle Demo
Edit for duplicate values
If you have duplicated rows for the int_proc
you have to add another sub case statement to check for repeated values and rank them accordingly
select
t2.id,
t2.customer_id,
t2.inventory,
t2.int_proc
from (
select t.*,
@r:= case when @g = t.customer_id
then
case when @sg = t.inventory
then
case when @sr <> t.int_proc
then @r+1
else @r end
else 1 end
else 1 end r,
@g:= t.customer_id g,
@sg:= t.inventory sg,
@sr:= t.int_proc sr
from test t
cross join (select @g:=null,@sg:=null,@r:=null,@sr:=null) t1
where t.customer_id in('A','B')
order by t.customer_id,t.inventory,t.int_proc desc
) t2
where t2.r <= 2
order by id
Fiddle Demo 2