你已经有一个布尔条件: "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