已售 SQL 票证 - 过去 30 天 - 使用地点或拥有?
从当天起过去 30 天内销售的航线,每条航线每天售出多少张机票?
SELECT COUNT(TICKET_ID) NUMBER_TICKETS, ROUTE_CODE, FLIGHT_DATE
FROM TICKETS
WHERE (DAYS(CURRENT DATE)- DAYS(FLIGHT_DATE))<=30
GROUP BY ROUTE_CODE, FLIGHT_DATE
这应该是 WHERE 还是 HAVING?我不确定哪种方式是正确的?
How many tickets were sold for each route and each day, for the routes sold in the last 30 days from current day?
SELECT COUNT(TICKET_ID) NUMBER_TICKETS, ROUTE_CODE, FLIGHT_DATE
FROM TICKETS
WHERE (DAYS(CURRENT DATE)- DAYS(FLIGHT_DATE))<=30
GROUP BY ROUTE_CODE, FLIGHT_DATE
Should this be WHERE or HAVING? I am not sure which way is correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要它在哪里。
以下是理解这一点的最佳方法 -
WHERE 子句在 GROUP BY 之前使用
HAVING 子句在 GROUP BY 之后使用
因此 - 您想要对过去 30 天内的项目进行分组 - 这发生在分组之前。
You want it in the WHERE.
Here is the best way to understand this --
The WHERE clause is used before the GROUP BY
The HAVING clause is used after the GROUP BY
So -- you want to group items in the last 30 days -- that happens before the group.
正确的命令在哪里,考虑使用“datediff”来计算 30 天
where is the right command, consider to use "datediff" for calculate 30 days
WHERE 子句用于在进行任何分组之前指定过滤记录的条件
HAVING 子句用于指定从组中过滤值的条件,
在这种情况下,where 子句用于过滤条件,而不是组。
例如,如果您想检查 Ticket_id,则应该使用“having”。
the WHERE clause is used to specify a condition for filtering records before any groupings are made
the HAVING clause is used to specify a condition for filtering values from a group
in this case, the where clause is used for filtering the condition, not the group.
You should use "having",for example, if you wanted to check the ticket_id.