子查询内部案例陈述建议

时间:2017-10-23 08:47:35

标签: sql oracle

我有以下查询,但是我想在case语句中使用子查询。我尝试了不同的东西,但没有快乐。请有人建议

select a.PARAMID, a.PERSONOPTID, a.paramdate,(case 
when (to_char(a.paramdate,'mmdd')>='0301' and to_char(a.paramdate,'mmdd')<='0620') then 'spring' --else 'Summer' 
when (to_char(a.paramdate,'mmdd')>='0621' and to_char(a.paramdate,'mmdd')<='0920') then 'Summer' -- else 'Autumn' 
when (to_char(a.paramdate,'mmdd')>='0921' and to_char(a.paramdate,'mmdd')<='1130') then 'Autumn'  else 'winter'
--when (to_char(a.paramdate,'mmdd')>'1201' and to_char(a.paramdate,'mmdd')<='0228') then 'winter'end 
end)  from personoptions a, personopt po, person 
WHERE a.PARAMID=190 and 
a.PERSONOPTID in (select a.PERSONOPTID from personoptions a
where  a.paramid=185 and 
trunc(a.PARAMDATE) between trunc(sysdate-372)and trunc(sysdate-365)) and 
a.PERSONOPTID = po.PERSONOPTID
AND po.PERSONID = person.personid
AND po.PERSONID  in ( 12345)

我尝试了什么,

select (case when exists (select personid from ( select person.personid from personoptions a, personopt po, person
WHERE a.PARAMID=190 and 
a.PERSONOPTID in (select a.PERSONOPTID from personoptions a
where  a.paramid=185 and 
trunc(a.PARAMDATE) between trunc(sysdate-372)and trunc(sysdate-365)) and 
a.PERSONOPTID = po.PERSONOPTID
AND po.PERSONID = person.personid
AND po.PERSONID  in ( 1481198891002120249232017090384761))) then  
(case when (to_char(a.paramdate,'mmdd')>='0301' and to_char(a.paramdate,'mmdd')<='0620') then 'spring' --else 'Summer' 
when (to_char(a.paramdate,'mmdd')>='0621' and to_char(a.paramdate,'mmdd')<='0920') then 'Summer' -- else 'Autumn' 
when (to_char(a.paramdate,'mmdd')>='0921' and to_char(a.paramdate,'mmdd')<='1130') then 'Autumn'  else 'winter'
--when (to_char(a.paramdate,'mmdd')>'1201' and to_char(a.paramdate,'mmdd')<='0228') then 'winter'end 
end)end) from person 

错误:Ora-00904:a.paramdate无效标识符

1 个答案:

答案 0 :(得分:0)

  1. 您不应该有两个具有相同别名的表(两个别名为'a'的表),因为数据库有时可能无法理解您并返回一些奇怪的结果
  2. 子查询中使用的表只能在其使用的()中访问。您试图在查询结尾的case语句中使用范围之外的表。