简体   繁体   English

可以 HAVING 评估字符串条件

[英]Can HAVING evaluate a string condition

The following query totals sales in state= 'MA' for each salesperson.

SELECT salesperson_id, sum(sales)
FROM salesperson
GROUP BY salesperson_id
HAVING state = 'MA';


Incorrect Response : TRUE 
    
Correct Answer : FALSE

[Source: Hoffer et al Modern Database Management 13 ed., practice question] [来源:Hoffer 等人现代数据库管理第 13 版,练习题]

The SQL statement apparently cannot display sales for state = 'MA' for each salesperson. SQL 语句显然无法显示每个销售人员 state = 'MA' 的销售额。 Without recreating this in SQL server, I imagined this will be ok.没有在 SQL 服务器中重新创建它,我想这会没问题。 Why would the answer be FALSE in this case?为什么在这种情况下答案会是 FALSE? Thanks!谢谢!

Context: I am going through some practices questions for the Hoffer et al textbook Modern Database Management (13 ed.), and I'm trying to understand the answer to this question.上下文:我正在研究 Hoffer 等教科书现代数据库管理(第 13 版)的一些练习题,我试图理解这个问题的答案。

If your purpose is to merely exclude any rows for any other state , you should place it in the WHERE clause.如果您的目的只是排除任何其他state的任何行,则应将其放在WHERE子句中。 HAVING applies to the rows constructed after the GROUP BY (you only have salesperson_id & sum(sales) in the GROUP BY, so HAVING can only have conditions for these). HAVING适用于 GROUP BY 之后构造的行(你在 GROUP BY 中只有salesperson_id & sum(sales) ,所以 HAVING 只能有这些条件)。

'state' must be in the GROUP BY for this to work; 'state' 必须在GROUP BY中才能工作; but this query is syntactically invalid.但是这个查询在语法上是无效的。 If you are not getting a syntax error it could be because state might be a reserved word in your dbms with a meaning/value other than 'MA', then you would get no errors, but no results, either.如果您没有收到语法错误,可能是因为state可能是您的 dbms 中的保留字,其含义/值不是“MA”,那么您不会收到任何错误,但也不会收到任何结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM