有条款错误 [英] Error with having clause

查看:25
本文介绍了有条款错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select SUM (Bill) from ProductSaleReport group by PCI 
having MONTH(Date) between 1 and 3

有人可以吗帮我找到问题.?

Could any one please help me finding the issue.?

我收到错误:

消息 8121,级别 16,状态 1,第 1 行
HAVING 子句中的ProductSaleReport.Date"列无效因为它既不包含在聚合函数中,也不包含在GROUP BY 子句.
消息 8121,级别 16,状态 1,第 1 行
HAVING 子句中的ProductSaleReport.Date"列无效因为它既不包含在聚合函数中,也不包含在GROUP BY 子句.

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) 不是您分组的列,因此它不能出现在 have 子句中.你可以这样做:

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 

另一种方式是

select SUM (Bill) 
from ProductSaleReport 
group by PCI, MONTH(Date) 
having MONTH(Date) between 1 and 3

但请记住,您将获得按月和 PCI 分组的结果.

but keep in mind that you will get result grouped by month as well as by PCI.

此处解释了 WHERE 和 HAVING 之间的区别:使用 'where子句中的case表达式列'

The difference between WHERE and HAVING explained here: Using 'case expression column' in where clause

这篇关于有条款错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆