My query works fine to get what i want but when I changed it a bit to streamline it, it started giving me an error and I am not quite sure why. The original query that works just fine is:
select g1.priceclassid, sum(p3.baseprice * (1 - (c1.discpct/100)) * p2.itemqty) as price, c1.volmin, c1.discpct, c1.voldiscpct
from comppriceclass as c1
inner join pomaster as p1 on p1.companyid = c1.companyid and p1.pricetype = c1.pricetype and pomasterid = 4263
inner join podetail as p2 on p2.pomasterid = p1.pomasterid
inner join gridmaster as g1 on g1.gridmasterid = p2.gridmasterid and g1.priceclassid = c1.priceclassid
inner join pricingmodels as p3 on p3.prodarrkey = p2.prodarrkey and p3.pricingmasterid = p1.histpriceid
where c1.volmin > 0 and c1.voldiscpct > 0
group by g1.priceclassid
having sum(p3.baseprice * (1 - (c1.discpct/100)) * p2.itemqty) > c1.volmin
Then when I run the following it gives me the error “#1054 Unknown column c1.volmin in having clause” where the only change I make is in the select part of the query:
select g1.priceclassid
from comppriceclass as c1
inner join pomaster as p1 on p1.companyid = c1.companyid and p1.pricetype = c1.pricetype and pomasterid = 4263
inner join podetail as p2 on p2.pomasterid = p1.pomasterid
inner join gridmaster as g1 on g1.gridmasterid = p2.gridmasterid and g1.priceclassid = c1.priceclassid
inner join pricingmodels as p3 on p3.prodarrkey = p2.prodarrkey and p3.pricingmasterid = p1.histpriceid
where c1.volmin > 0 and c1.voldiscpct > 0
group by g1.priceclassid
having sum(p3.baseprice * (1 - (c1.discpct/100)) * p2.itemqty) > c1.volmin
When I add the c1.volmin back into the select clause it runs fine again as:
select g1.priceclassid, c1.volmin
from comppriceclass as c1
inner join pomaster as p1 on p1.companyid = c1.companyid and p1.pricetype = c1.pricetype and pomasterid = 4263
inner join podetail as p2 on p2.pomasterid = p1.pomasterid
inner join gridmaster as g1 on g1.gridmasterid = p2.gridmasterid and g1.priceclassid = c1.priceclassid
inner join pricingmodels as p3 on p3.prodarrkey = p2.prodarrkey and p3.pricingmasterid = p1.histpriceid
where c1.volmin > 0 and c1.voldiscpct > 0
group by g1.priceclassid
having sum(p3.baseprice * (1 - (c1.discpct/100)) * p2.itemqty) > c1.volmin
So why does it need c1.volmin in the select clause but not c1.discpct or p2.itemqty or p3.baseprice, all of which are also used in the having clause?
Thanks for the help