Issue in using distinct to show a specific record

My index has products and some products have variants also.
So, my data looks something like this:

  • {sku=1, basesku=1, name=Prdt1}
  • {sku=2, basesku=2, name=Prdt2}
  • {sku=3, basesku=2, name=Prdt3}
  • {sku=4, basesku=2, name=Prdt4}

I am deduplicating the records on basesku attribute.

The requirement is that when we show the listing page, the base products should be shown - so this is mainly applicable in case where there are variants (i.e products which have the same basesku).
For this, I am deduplicating on basesku.
Also the display should be as below:

  • Prdt 1
  • Prdt 2
    mainly because Prdt 2 is the basesku for Prdt3 and Prdt4, so the variants should not show up but only the base product should show up.

But I noticed that when I do this, there are some products in which the base product is not coming up but instead any variant comes up. But i specifically want to show only the base product.
Any pointers on how this can be achieved will really be helpful.