SELECT x.BillCode,x.BillDate,m.Code,m.Name,y.GoodsIdCode,y.GoodsIdName,y.PackageIdName,y.Price,y.LineDisc ,
y.NotUsedQ,(y.NotUsedQ - y.BackQ),y.Price*(y.NotUsedQ -
y.BackQ)*y.LineDisc as Money,y.UsedQ,y.Quantity,x.billcode,y.ToDate,y.nurseid,y.nurseid2,nurseid3
FROM tPosRetailNM x
LEFT JOIN tPosRetailND y ON x.KeyId=y.MasterId
LEFT JOIN tMember m ON x.MenberId=m.KeyId
LEFT JOIN tShop s ON x.ShopId = s.KeyId
Where (y.NotUsedQ - y.BackQ > 0) and (y.NotUsedQ > 0)
and EXISTS (SELECT rd.keyid FROM
tPosRetailD rd left join tPosRetailm rm on rd.Masterid = rm.keyid WHERE(rd.KeyId = y.FromDetailId)
AND ((rd.Money <= rd.NurseId3 + rd.CommM + 0.1) or (rm.money =
rm.payed))) --and ( s.code>='CD0008' ) and (s.code<='CD0008' )
--and y.NotUsedQ<>y.Quantity
order by x.billdate
SELECT sum(y.Quantity) Quantity,sum(NotUsedQ) NotUsedQ,sum(y.Price) Price
--y.Price,
--(y.NotUsedQ - y.BackQ),y.UsedQ,y.Quantity
FROM tPosRetailNM x LEFT JOIN tPosRetailND y ON x.KeyId=y.MasterId LEFT
JOIN tMember m ON x.MenberId=m.KeyId LEFT JOIN tShop s ON x.ShopId = s.KeyId
Where (y.NotUsedQ - y.BackQ > 0) and (y.NotUsedQ > 0) and EXISTS (SELECT rd.keyid FROM
tPosRetailD rd left join tPosRetailm rm on rd.Masterid = rm.keyid WHERE(rd.KeyId = y.FromDetailId)
AND ((rd.Money <= rd.NurseId3 + rd.CommM + 0.1) or (rm.money =
rm.payed))) ---and ( s.code>='CD0002' ) and (s.code<='CD0002' )