具有多个“when”的案例陈述...不起作用!
这是我正在做的事情的简化版本,但我无法让任何东西发挥作用。该语句给了我一个错误,“ERR”后没有逗号。我希望该栏为“月”,我认为这会起作用,但我遇到了很多麻烦。感谢您的帮助!
select a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Days from reporting a inner join Repository b on a.RECORD_ID = b.RECORD_ID where a.POL_OGN_EFF_DT >= '2008-11-01' group by a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Days
This is a simplified version of what I'm doing, but I can't get anything to work. The statement gives me an error without the comma after 'ERR'. I want the column to be 'Month' and I tohught this would work but I'm having a ton of trouble. Thanks for your help!
select a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Days from reporting a inner join Repository b on a.RECORD_ID = b.RECORD_ID where a.POL_OGN_EFF_DT >= '2008-11-01' group by a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Days
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试不使用逗号...下面的示例。
Try it without commas... example to follow.
修复逗号:
Fix the commas :
将
Day
和Year
放在方括号中,如下所示:Put square brackets around
Day
andYear
, like this:您不能在
GROUP BY
中使用AS
别名。该表达式应与 SELECT 中不带别名的表达式匹配。You cannot use an
AS
alias in theGROUP BY
. The expression should match the expression in your SELECT without the alias.解决有关“多部分标识符“日”的新错误消息,
表中是否有“日”和“年”列?
您的查询中 From 之后是什么?您是否要将多个表连接在一起?请显示整个查询?
好的,根据您编辑的问题,(您不能在分组依据中使用别名)尝试这个:
但实际上,您根本没有聚合函数,只有选择中每个表达式的分组依据,所以所有你需要的是一个独特的关键字,你根本不需要分组依据:
Addressing your new error msg about 'multi-part identifier 'day',
Are
day
andyear
columns in the table ?What comes after the From in your query ? Are you joining multiple tables together in this? Please show the entire query?
ok, based on yr edited question, (You can't use an alias in a Group By) try this:
but actually, you have no aggregate functions in there at all, just a group by on every expression in the select, so all you need is a Distinct key word , you don;t need the group by at all:
我认为案件结束后你错过了“AS”。
I think you are missing 'AS' after the end of the case.
注意:这是问题评论中调试会话的结果。
错误
关键字“as”附近的语法不正确。
是由as Policy_Years
引起的GROUP BY
子句中的 code>。不允许在GROUP BY
子句中使用as
。Note: This is the outcome of the debugging session in the question comments.
The error
Incorrect syntax near the keyword 'as'.
was caused byas Policy_Years
in theGROUP BY
clause. You are not allowed to useas
within aGROUP BY
clause.