具有多个“when”的案例陈述...不起作用!

发布于 2024-08-19 23:47:26 字数 647 浏览 4 评论 0原文

这是我正在做的事情的简化版本,但我无法让任何东西发挥作用。该语句给了我一个错误,“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

生生漫 2024-08-26 23:47:27

尝试不使用逗号...下面的示例。

select  
   case 
      when a.month is NULL then 'ERR'
      when a.month > '011' then '12' 
      else a.month 
   end as Month, 
   a.Last7Days 
from ... 

Try it without commas... example to follow.

select  
   case 
      when a.month is NULL then 'ERR'
      when a.month > '011' then '12' 
      else a.month 
   end as Month, 
   a.Last7Days 
from ... 
凉城 2024-08-26 23:47:27
select 
     day, year,
case
   when a.month is NULL then 'ERR'
   when a.month > '011' then '12'
   else a.month end as Month,
 a.Last7Days
select 
     day, year,
case
   when a.month is NULL then 'ERR'
   when a.month > '011' then '12'
   else a.month end as Month,
 a.Last7Days
请恋爱 2024-08-26 23:47:27

修复逗号:

select 
     day, year,
     case
       when a.month is NULL then 'ERR'
       when a.month > '011' then '12'
       else a.month 
     end Month,
     a.Last7Days
from [table]

Fix the commas :

select 
     day, year,
     case
       when a.month is NULL then 'ERR'
       when a.month > '011' then '12'
       else a.month 
     end Month,
     a.Last7Days
from [table]
慵挽 2024-08-26 23:47:27

DayYear 放在方括号中,如下所示:

select a.[Day], a.[Year], ...

Put square brackets around Day and Year, like this:

select a.[Day], a.[Year], ...
z祗昰~ 2024-08-26 23:47:27

您不能在 GROUP BY 中使用 AS 别名。该表达式应与 SELECT 中不带别名的表达式匹配。

You cannot use an AS alias in the GROUP BY. The expression should match the expression in your SELECT without the alias.

GRAY°灰色天空 2024-08-26 23:47:27

解决有关“多部分标识符“日”的新错误消息,

表中是否有“日”和“年”列?
您的查询中 From 之后是什么?您是否要将多个表连接在一起?请显示整个查询?

好的,根据您编辑的问题,(您不能在分组依据中使用别名)尝试这个:

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 
   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,
   a.Last7Days, a.Last30Days, a.Last90Days

但实际上,您根本没有聚合函数,只有选择中每个表达式的分组依据,所以所有你需要的是一个独特的关键字,你根本不需要分组依据:

select Distinct 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 
   join Repository b 
      on a.RECORD_ID = b.RECORD_ID
where a.POL_OGN_EFF_DT >= '2008-11-01'

Addressing your new error msg about 'multi-part identifier 'day',

Are day and year 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:

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 
   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,
   a.Last7Days, a.Last30Days, a.Last90Days

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:

select Distinct 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 
   join Repository b 
      on a.RECORD_ID = b.RECORD_ID
where a.POL_OGN_EFF_DT >= '2008-11-01'
红焚 2024-08-26 23:47:27

我认为案件结束后你错过了“AS”。

I think you are missing 'AS' after the end of the case.

桜花祭 2024-08-26 23:47:26

注意:这是问题评论中调试会话的结果。

错误关键字“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 by as Policy_Years in the GROUP BY clause. You are not allowed to use as within a GROUP BY clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文