ORA-01427:单行子查询在显示输出时返回多行

时间:2019-04-29 11:16:54

标签: sql oracle subquery

我只想显示3条带有特定输出条件的输出,而不是整个查询。所以我在select语句中使用了子查询。

我尝试了以下代码

--CREATE TABLE i86813_dt190429 as 
                   SELECT 
                   /*+ use_hash(RAP01 RAA02 RAP06) */ 
                   DISTINCT 'I86813' AS audit_id,
                   rap01.plcy                 AS plcy,
                   rap01.stuscd,
                   raa02.enddt_t              AS enddt,
                   rap01.j01_pt_line_cat_cd   AS j01_pt_line_cat_cd,
                   rap01.j01_pt_cdb_part_id   AS j01_pt_cdb_part_id,
                   rap01.j01_pt_state_cd      AS j01_pt_state_cd,
                   rap06.sctype,
                   raa02.EACPRC,

                   (SELECT rap06.pstsc
                     FROM rap06
                     JOIN raa02
                   ON rap06.plcy = raa02.plcy
                      AND raa02.enddt_t - 1 BETWEEN rap06.enddt_t AND ( rap06.dropdt_t - 1 )
                   ) AS pstsc_before_ea  --one day before EA
                   ,
                   (
                   SELECT rap01.aap
                     FROM rap01
                     JOIN raa02
                   ON rap01.plcy = raa02.plcy
                      AND raa02.enddt_t - 1 = rap01.enddt_t
                   ) AS aap_before_ea        --one day before EA and after EA
                   ,
                   (
                   SELECT rap01.aap
                     FROM rap01
                     JOIN raa02
                   ON rap01.plcy = raa02.plcy
                      AND raa02.enddt_t > rap01.enddt_t
                   ) AS aap_after_ea
--
from RAP01
--
Join RAA02 
ON raa02.j46_pt_line_cat_cd    = rap01.j01_pt_line_cat_cd
AND raa02.j46_pt_cdb_part_id   = rap01.j01_pt_cdb_part_id
AND raa02.j46_pt_state_cd      = rap01.j01_pt_state_cd
AND raa02.plcy                 = rap01.plcy
AND raa02.EACPRC               = '25'                    --channel of processing.
AND raa02.ahevnt               = '0993'                  -- ??
and raa02.sprodt_t             BETWEEN '13-AUG-2018' and '14-APR-2019'
--
left JOIN RAP06
ON RAP06.J42_PT_LINE_CAT_CD    = RAP01.J01_PT_LINE_CAT_CD
AND RAP06.J42_PT_CDB_PART_ID   = RAP01.J01_PT_CDB_PART_ID
AND RAP06.J42_PT_STATE_CD      = RAP01.J01_PT_STATE_CD
AND RAP06.PLCY                 = RAP01.PLCY
AND RAP06.SCTYPE               = '085'
AND RAA02.enddt_t              BETWEEN RAP06.ENDDT_T AND  (RAP06.DROPDT_T  - 1)     
--
WHERE rap01.j01_pt_line_cat_cd = 'A'
AND rap01.co3 || rap01.line3 IN (
'065010',
'010010',
'027010',
'021010',
'386010',
'065019',
'010019',
'027019',
'021019',
'386019'
)
AND RAP06.PLCY is NULL;

由于“ ORA-01427:单行子查询返回多个行”而出现错误 和'01427。 00000-“单行子查询返回多个行”'

能否请您提出解决方案。

2 个答案:

答案 0 :(得分:0)

如果在主选择中使用子查询,则它应返回单个值。看来您正在通过日期范围过滤器,该过滤器正在重新调整多行。

AND raa02.enddt_t - 1 BETWEEN rap06.enddt_t AND ( rap06.dropdt_t - 1 )

您可以将这些子查询作为派生表连接到基表。

答案 1 :(得分:0)

在select语句中,您有3个子查询来填充列pstsc_before_eaaap_before_eaaap_after_ea

.....
                   (SELECT rap06.pstsc
                     FROM rap06
                     JOIN raa02
                   ON rap06.plcy = raa02.plcy
                      AND raa02.enddt_t - 1 BETWEEN rap06.enddt_t AND ( rap06.dropdt_t - 1 )
                   ) AS pstsc_before_ea  --one day before EA
                   ,
                   (
                   SELECT rap01.aap
                     FROM rap01
                     JOIN raa02
                   ON rap01.plcy = raa02.plcy
                      AND raa02.enddt_t - 1 = rap01.enddt_t
                   ) AS aap_before_ea        --one day before EA and after EA
                   ,
                   (
                   SELECT rap01.aap
                     FROM rap01
                     JOIN raa02
                   ON rap01.plcy = raa02.plcy
                      AND raa02.enddt_t > rap01.enddt_t
                   ) AS aap_after_ea

其中每个应该返回仅1行
但是看起来其中至少有1个返回更多。
您可以使用类似的方法避免错误:

SELECT max(rap01.aap)...

如果这符合您的逻辑。