ML
select distinct b.Mobile, top3.PCatID
from Buyers b
inner join(
select top 3 b1.Mobile, s.PCatID
from Buyers b1
inner join _DCards d on d.BuyerID=b1.BuyerID
inner join Transactions t on t.CardID=d.CardID
inner join TransactionItems tr on tr.TransactionId=t.TransactionId
inner join _SKU s on s.SKU=tr.SKU
where t.DocDate between '20190101' and '20200225' and t.TransactionTypeID in (2,3)
group by b1.Mobile, s.PCatID
order by COUNT (Distinct t.TransactionId)
) as top3 on top3.Mobile=b.Mobile
order by b.Mobile