案例陈述中的错误消息

时间:2013-09-24 10:57:06

标签: plsql

由于我添加了In子句,我写了以下语句不起作用。有人可以帮助提出可行的解决方案吗?

我想要做的是测量每个记录键的成绩的最小/最大值。有些人有不止一个成绩,这就是为什么我添加了In条款以便它会考虑这个因素?

case when MIN("Grade - Current"."Grade Equivalency")=MIN("Grade - Current"."Grade Equivalency") 
IN (People." Record Key") then 'y' else 'n' end

表中的数据如下所示:

RecordKey版本等级

165 2009 1

165 2012 2

175 2009 1

189 2012 1

200 2009 2

200 2012 1

1 个答案:

答案 0 :(得分:0)

您已经有一个布尔条件: “MIN(”Grade - Current“。”Grade Equivalency“)= MIN(”Grade - Current“。”Grade Equivalency“)”在你的时间,所以你也不能同时做一个IN条款。

当您在括号中使用IN子句时,您必须放置值而不是表列。

也许你需要这样的东西:

case
when MIN("Grade - Current"."Grade Equivalency")=MIN("Grade - Current"."Grade Equivalency") AND MIN("Grade - Current"."Grade Equivalency") IN (one or more values...) then 'y' 
else 'n' 
end

第二个答案......

它更复杂,因为它使用分析功能。

看看下面的内容:

select RECORDKEY, VERSION_YEAR, GRADE,
case
  when GRADE_PREV = 0 and GRADE_NEXT >= 0 then 'Stayed the same'
  when GRADE_PREV = 0 and GRADE_NEXT > 0 then 'Stayed the same'
  when GRADE_PREV > 0 and GRADE_PREV < GRADE then 'increased'
  when GRADE_PREV > 0 and GRADE_PREV > GRADE then 'decreased'
  else null
end as grade_change
from
(
  select RECORDKEY, VERSION_YEAR, GRADE
  ,LAG(GRADE, 1, 0) over (partition by RECORDKEY order by RECORDKEY, VERSION_YEAR) as GRADE_PREV
  ,LEAD(GRADE, 1, 0) over (partition by RECORDKEY order by RECORDKEY, VERSION_YEAR) as GRADE_NEXT
  from PLCH_GRADES
)
order by 1,2

第三个答案......

现在整合您的“原始”查询:

select RECORDKEY, CALENDAR_YEAR, HEADCOUNT, GRADE,
case
  when GRADE_PREV = 0 and GRADE_NEXT >= 0 then 'Stayed the same'
  when GRADE_PREV = 0 and GRADE_NEXT > 0 then 'Stayed the same'
  when GRADE_PREV > 0 and GRADE_PREV < GRADE then 'increased'
  when GRADE_PREV > 0 and GRADE_PREV > GRADE then 'decreased'
  else NULL
end as GRADE_CHANGE
from
(
  select RECORDKEY, CALENDAR_YEAR, HEADCOUNT, GRADE
  ,LAG(GRADE, 1, 0) over (partition by RECORDKEY order by RECORDKEY, VERSION_YEAR) as GRADE_PREV
  ,LEAD(GRADE, 1, 0) over (partition by RECORDKEY order by RECORDKEY, VERSION_YEAR) as GRADE_NEXT
  from 
  (
    select 
      PEOPLE."Record Key" as RECORDKEY, 
      CALENDAR.year as CALENDAR_YEAR, 
      "Grade - Current"."Grade Equivalency" as GRADE, 
      "Fact - People".HEADCOUNT  
    from test 
    where location = 'NI'
    and PEOPLE."Status Group" = 'CURRENT'
    and PEOPLE."Headcount Marker" in ('Paid', 'Unpaid')
    AND Calendar.Year IN ('2009', '2012') 
  )
)
order by 1,2