Oracle SQL - 带有迭代的 Case 语句

Posted

技术标签:

【中文标题】Oracle SQL - 带有迭代的 Case 语句【英文标题】:Oracle SQL - Case statement with iteration 【发布时间】:2020-02-04 06:52:00 【问题描述】:

应将 ASCII 表值与 s.manure_type 进行比较。对于下表中的每条记录,QuantityText case 语句应该做一个比较。它需要选择的值是例如燕麦,天鹅绒豆,其他没有。

select                        
c.id customer_num,
c.type type, 
s.id_text sample_num,
c.sasa_grower_code s_grower,
c.address s_address1,
c.postalcode s_post_code,
c.email q1_email,
nvl(c.client_name, c.farm_name )s_company,
c.farm_name s_estate,
c.contact_name s_contact,
s.id_numeric id_numeric,
s.id_text fas_lab_id,
s.date_received received_date,
s.date_printed printed_date,
s.sampled_date sampled_date,
e.name S_AREA_DESCRIP,
a.name s_advisor_name,
a.email s_advisor_email,
s.order_no s_order_num,
s.field_name s_field,
p.phrase_text  || ' cm' sample_depth,
cr.crop_name s_crop,
s.attyield s_yield,
s.variety s_varty, 
case when s.flg_trashed is null then 
               'None'
             else (case when s.flg_trashed = constant_pkg.get_true  then 
                     'Yes'
                   else (case when s.flg_trashed = constant_pkg.get_false then 
                          'No'
                         else ' ' 
                         end)
                   end) end trashed,
case when s.flg_irrigated is null then 
               'None'
             else (case when s.flg_irrigated = constant_pkg.get_true then 
                     'Yes'
                   else (case when s.flg_irrigated = constant_pkg.get_false then 
                          'No'
                         else ' ' 
                         end)
                   end) end s_irrig,     
                   CASE
                        WHEN trim(s.manure_type) in (select p.phrase_id from phrase p where p.phrase_type = 'AL_G_MANUR') then (select p.phrase_text from phrase p)
                    END AS QuantityText,                  
'' S_GM_YIELD,
s.project_code project_code,
s.trial_ref trial_ref,
s.cost_centre cost_centre
from client c
left outer join sample s on (s.client_id = c.id)
left outer join extension e on (e.id = c.extension_id)
left outer join advisor a on (a.id = c.advisor_id)
left outer join phrase p on (p.phrase_id = s.depth)
left outer join crop cr on (cr.id = s.crop_id)
where p.phrase_type = phrase_pkg.get_soil_depth
and c.id =    '211493A'
and s.fas_sample_type =  sample_pkg.get_soil_sample
and s.flg_recommendation = sample_pkg.get_flg_recommendation
and s.id_numeric between   14932  and  14933
+----------------------------+
|           Phrase           |
+----------------------------+
|AL_G_MANUR  OA Oats         |
|AL_G_MANUR  V  Velvet Beans
|AL_G_MANUR  O  Other
|AL_G_MANUR  N  None         |
+----------------------------+

但我收到错误 ORA-00900:单行查询返回多于一行

【问题讨论】:

问题出在最后一个 CASE 表达式中。 【参考方案1】:

其中一个 case 语句中缺少 where 子句很可能是原因。

CASE
  WHEN trim(s.manure_type) in 
      (select p.phrase_id from phrase p where p.phrase_type = 'AL_G_MANUR') 
    then (select p.phrase_text from phrase p) <<< NO WHERE CLAUSE ?
END AS QuantityText,                  

这相对容易自己调试。

    删除单个选定的列 检查是否仍然出现错误 如果是,请返回 1。 如果没有,则验证为什么最后添加的列错误

【讨论】:

【参考方案2】:

问题出在下面的语句中

then (select p.phrase_text from phrase p)

我猜,应该换成这个

(select p.phrase_text from phrase p where p.phrase_type = 'AL_G_MANUR')

【讨论】:

以上是关于Oracle SQL - 带有迭代的 Case 语句的主要内容,如果未能解决你的问题,请参考以下文章

oracle case语数据类型不一致问题

Oracle SQL中的Merge用法

SQL Case 语句子选择内的子选择分组

LIKE 在 CASE 语句中 Oracle SQL (Sql Developer)

SQL中CASE WHEN THEN的用法

oracle 中 sql 应用case when 提示数字无效问题。高手指点迷津!!!!