SQL query error: SELECT p.ProductID, ProdnameE, ProdNameT , SizeW, SizeD,SizeH,PriceUnit,ColorNameE,MakerNameE,RankName,L,M,L_T,M_T,case when ifnull(PSTSH1.Shop1Qty,0) < 0 then 0 else ifnull(PSTSH1.Shop1Qty,0) end Shop1Qty, case when ifnull(PSTSH1.Shop1Qty,0) < 0 then PSTSH1.Shop2Qty+PSTSH1.Shop1Qty else ifnull(PSTSH1.Shop2Qty,0) end Shop2Qty,ProdCatGrpNo,ProdCatMdlNo,PriceNet, case when ifnull(PSTSH1.Shop3Qty,0) < 0 then 0 else ifnull(PSTSH1.Shop3Qty,0) end Shop3Qty, case when ifnull(PSTSH1.Shop4Qty,0) < 0 then 0 else ifnull(PSTSH1.Shop4Qty,0) end Shop4Qty FROM (select * from mstproduct_n where DelFlag=0 ) as p Left Join (select productid,sum(Shop1Qty) Shop1Qty,sum(Shop2Qty) Shop2Qty,sum(Shop3Qty) Shop3Qty,sum(Shop4Qty) Shop4Qty from ( select ProductID, case when ShopID=1 and LocationID <> 4 then AvailableQty else 0 end Shop1Qty, case when ShopID=2 then AvailableQty else 0 end Shop2Qty, case when ShopID=3 then AvailableQty else 0 end Shop3Qty, case when ShopID=1 and LocationID = 4 then AvailableQty else 0 end Shop4Qty from PrgStock) as b group by productid ) PSTSH1 on p.ProductID=PSTSH1.ProductID WHERE ifnull(PSTSH1.Shop2Qty,0)+ifnull(PSTSH1.Shop1Qty,0) +ifnull(PSTSH1.Shop3Qty,0) +ifnull(PSTSH1.Shop4Qty,0) > 0 AND ProdCatGrpNo = 4 AND ProdCatMdlNo = 3 ORDER BY p.SPrice ASC,p.ProductID ASC Limit 0,20