select SUM (Bill) from ProductSaleReport group by PCI having MONTH(Date) between 1 and 3
有人可以帮我发现问题吗?
我得到了错误:
消息8121,级别16,状态1,行1 列“ ProductSaleReport.Date”在HAVING子句中无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。 消息8121,级别16,状态1,行1 列“ ProductSaleReport.Date”在HAVING子句中无效,因为它既不在聚合函数中也不在GROUP BY子句中。
MONTH(Date)不是您分组的列,因此它不会出现在Have子句中。您可以这样做:
select SUM (Bill) from ProductSaleReport where MONTH(Date) between 1 and 3 group by PCI
其他方式是
select SUM (Bill) from ProductSaleReport group by PCI, MONTH(Date) having MONTH(Date) between 1 and 3
但请记住,您将获得按月以及按PCI分组的结果。