ORA-01427: 单行子查询在显示输出时返回多行
Posted
技术标签:
【中文标题】ORA-01427: 单行子查询在显示输出时返回多行【英文标题】:ORA-01427: single-row subquery returns more than one row while displaying the output 【发布时间】:2019-04-29 11:16:53 【问题描述】:我想显示 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 - “单行子查询返回多于一行”'
能否请您提出解决方案。
【问题讨论】:
如果您在主选择中使用子查询,那么它应该返回单个值。 test Group by 并且不要对这个查询使用 distinct... "test Group by and don't use distinct for this query..." YukGROUP BY
建议数据去重且不使用聚合函数,但我猜它口味问题.. 另外 topicstarter 我建议您阅读 Why should I provide an MCVE for what seems to me to be a very simple SQL query? 以提供示例数据和预期结果,如果您想要更好的 cmets 和/或答案。
【参考方案1】:
如果您在主选择中使用子查询,那么它应该返回单个值。看起来您正在通过重新调整多行的日期范围过滤器。
AND raa02.enddt_t - 1 BETWEEN rap06.enddt_t AND ( rap06.dropdt_t - 1 )
您可以将这些子查询作为派生表连接到基表。
【讨论】:
【参考方案2】:在您的选择语句中,您有 3 个子查询来填充列 pstsc_before_ea
、aap_before_ea
和 aap_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)...
如果这符合你的逻辑。
【讨论】:
我不能在我的逻辑中使用任何聚合函数。所以这行不通。以上是关于ORA-01427: 单行子查询在显示输出时返回多行的主要内容,如果未能解决你的问题,请参考以下文章
得到以下错误 ORA-01427: 单行子查询返回多于一行 01427. 00000 - “单行子查询返回多于一行”