I have a table with a lot of bills. And I need to select the items from the bills where the bill excedes a price.
I tried this :
SELECT d.BillNumber
, SUM(d1.Amount* d1.Price)
, d2.Name AS FinalPrice
FROM GEMsc106Antet d LEFT OUTER JOIN GEMsc106Pozitii d1
ON d1.Luna = d.Luna AND d1.NumarI = d.NumarI
JOIN GEcProduse d2 ON d2.Cod = d1.CodMaterial
WHERE YEAR(d.Data) = 2013
GROUP BY d.BillNumber , d2.Name
HAVING SUM(d1.Amount* d1.Price) >= 10000
But this statement seems not to do the trick, as first it selects me only the bill from2013 which is OK, but then I should get all the bills that are greater then 10.000, and I can't use the sum in the where clause and only after that I should GROUP them.
What can I do?