SELECT CompanyID, CompanyName, Add1, Add2, Add3, CityNameCo , CountryID, Pincode, TelStdCode, TelNo, FaxStdCode, FaxNo, email, website, Activities, isElite, eliteuserfilepath, EliteEnteredDate, EliteValidTillDate, producttitle, ProductDesc, productimagefilepath, eliteConfirmed, CompanyProdID , CountryNameCo , InternetTypeCode, brandimage, brandname, countryshortcodeco , score, '' as CategoryList , dontshowincountries, Google_store_applink, Apple_store_applink,price,pricecurrency,productrating,whatsappnoforchat FROM ( SELECT CompanyID, CompanyName, Add1, Add2, Add3, CityNameCo , CountryID, Pincode, TelStdCode, TelNo, FaxStdCode, FaxNo, email, website, Activities, isElite, eliteuserfilepath, EliteEnteredDate, EliteValidTillDate, producttitle, ProductDesc, productimagefilepath, eliteConfirmed, CompanyProdID , CountryNameCo , InternetTypeCode, brandimage, brandname, countryshortcodeco , score, '' as CategoryList , dontshowincountries, Google_store_applink, Apple_store_applink,price,pricecurrency,productrating,whatsappnoforchat , 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 ('cultivator spare part') > 6 and eliteConfirmed = 1 and ( AfricaElite='1') and b.Productid='3719' limit 0, 250 ) 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 ( AfricaElite='1') and b.Productid='3719' limit 0, 250 ) 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 , 40 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('"cultivator spare part"') > 10 and eliteConfirmed = 1 and ( AfricaElite='1') limit 0, 250 ) 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 ='45' and b.Productid='3719' and isElite = 0 AND EliteValidTillDate < '2025-06-27' limit 0, 250 ) 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 , match (producttitle) against ('cultivator spare part') + 2 as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('cultivator spare part') > 6 and isElite=0 and a.Countryid ='45' and b.Productid='3719' limit 0, 250 ) 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 , match (producttitle) against ('"cultivator spare part"') as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('"cultivator spare part"') > 6 and isElite = 0 and a.Countryid ='45' limit 0, 250 ) 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 , match (producttitle) against ('cultivator spare part') as score from mstcompany_forsearch a inner join trncompanyprod_forsearch b on a.companyid=b.companyid where match (producttitle) against ('cultivator spare part') > 6 and isElite = 0 and a.Countryid ='45' limit 0, 250 ) 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 ='45' and match(activities) AGAINST ('"cultivator spare part"') > 10 limit 0, 250 ) 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 ('cultivator spare part') > 15 limit 0, 250 ) ) AS unioned ) AS ranked WHERE rn = 1 ORDER BY score desc ,EliteConfirmed desc , InternetTypeCode asc , productimagefilepath desc