1

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

4

1 回答 1

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
于 2013-09-24T12:57:54.817 回答