SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY CompanyID ORDER BY score DESC, eliteConfirmed DESC, InternetTypeCode ASC) AS rn FROM ( ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 65 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('pfa lined reducing tee') > 6 and eliteConfirmed = 1 and ( GCCElite='1') and b.Productid='4846' limit 0, 1000 } UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 50 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where eliteConfirmed = 1 and ( GCCElite='1') and b.Productid='4846' limit 0, 1000 } UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 15 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where a.Countryid ='7' and b.Productid='4846' and isElite = 0 AND EliteValidTillDate < '2025-06-26' limit 0, 1000 ) UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat ,14 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where isElite=0 and a.Countryid ='7' and b.Productid='4846' limit 0, 1000 ) UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 12 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('pfa lined reducing tee') > 6 and isElite = 0 and a.Countryid ='7' limit 0, 1000 ) UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 7 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where a.Countryid ='7' and match(activities) AGAINST ('"pfa lined reducing tee"') > 7 limit 0, 1000 ) UNION ALL ( select a.CompanyID,CompanyName,Add1,Add2,Add3,CityNameCo,a.CountryID,Pincode,TelStdCode,TelNo,FaxStdCode,FaxNo,email,website,Activities,isElite, eliteuserfilepath,EliteEnteredDate,EliteValidTillDate,producttitle,ProductDesc,productimagefilepath,eliteConfirmed ,CompanyProdID, CountryNameCo,InternetTypeCode,'' as brandimage,'' as brandname,countryshortcodeco,dontshowincountries,Google_store_applink,Apple_store_applink,price,pricecurrency, productrating,whatsappnoforchat , 6 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match(activities) AGAINST ('pfa lined reducing tee') > 15 limit 0, 1000 ) ) AS unioned ) AS ranked WHERE rn = 1 ORDER BY score desc ,EliteConfirmed desc , InternetTypeCode asc , c.productimagefilepath desc