select SUM (Bill) from ProductSaleReport group by PCI
having MONTH(Date) between 1 and 3
Could any one please help me finding the issue.?
I am getting the errors:
Msg 8121, Level 16, State 1, Line 1
Column 'ProductSaleReport.Date' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8121, Level 16, State 1, Line 1
Column 'ProductSaleReport.Date' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
MONTH(Date) is not a column you're grouped by, so it can't appear in having clause. You can do like that:
select SUM (Bill)
from ProductSaleReport
where MONTH(Date) between 1 and 3
group by PCI
Other way is
select SUM (Bill)
from ProductSaleReport
group by PCI, MONTH(Date)
having MONTH(Date) between 1 and 3
but keep in mind that you will get result grouped by month as well as by PCI.
The difference between WHERE and HAVING explained here: Using 'case expression column' in where clause
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With