在Oracle中的select语句中使用select

时间:2014-01-29 15:10:58

标签: sql oracle oracle11g oracle10g oracle-sqldeveloper

我正在尝试执行以下查询:

SELECT pt.prod_desc"Description",
(
 CASE
  WHEN pt.prod_level='2'
THEN 'Product'
WHEN pt.prod_level='4'
THEN 'Sub-Product'
WHEN pt.prod_level='5'
THEN 'Service'
ELSE 'N/A'
END)"Level", prod_id"CKC ID", isactive"Active", updt_usr_sid"Last Updated By", 
updt_ts"LAst Updated Date",
(CASE WHEN pt.prod_level='5' THEN parent_prod_id ELSE null END)"Parent Sub-Product CKC ID",
(CASE WHEN Level='Service' THEN (Select prod_desc from dims_prod_type where prod_id= parent_prod_id) ELSE 'N/A' END)
FROM dims_prod_type pt
  ORDER BY prod_desc;

我收到以下错误: 错误报告:

SQL Error: ORA-01788: CONNECT BY clause required in this query block
01788. 00000 -  "CONNECT BY clause required in this query block"
*Cause:    
*Action:

我将查询修改为以下内容:

 SELECT pt.prod_desc"Description",
 (
 CASE
  WHEN pt.prod_level='2'
THEN 'Product'
WHEN pt.prod_level='4'
THEN 'Sub-Product'
WHEN pt.prod_level='5'
THEN 'Service'
ELSE 'N/A'
END)"Level", prod_id"CKC ID", isactive"Active", updt_usr_sid"Last Updated By", 
updt_ts"LAst Updated Date",
(CASE WHEN pt.prod_level='5' THEN parent_prod_id ELSE null END)"Parent Sub-Product CKC   ID",
(CASE WHEN Level='Service' THEN (Select prod_desc from dims_prod_type where connect by prior prod_id=parent_prod_id) ELSE 'N/A' END)
FROM dims_prod_type pt
  ORDER BY prod_desc;

仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:1)

你在这里犯了两个错误:

  1. 您使用level Oracle保留关键字
  2. 调用了一个列
  3. 您在使用case / when创建的相同上下文中引用level列:该列不作为该范围中的独立列存在,并且不能在where / group by子句中引用,只是按条款排列