Sql 单行子查询在执行select语句时返回select中的多行
我在运行下面的SQL查询时遇到ORA-01427错误。我不确定我需要在下面的子查询中使用哪个聚合函数来解决这个问题Sql 单行子查询在执行select语句时返回select中的多行,sql,oracle,Sql,Oracle,我在运行下面的SQL查询时遇到ORA-01427错误。我不确定我需要在下面的子查询中使用哪个聚合函数来解决这个问题 select distinct x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s, y.les_s, y.client_s_name_s, z.DLR_S, k.nam_long_s, z.ID_S, z.res_d, y.short_add_s, y.short_cty_s, y.st_s, y.bus_
select distinct
x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s,
y.les_s, y.client_s_name_s,
z.DLR_S, k.nam_long_s, z.ID_S, z.res_d, y.short_add_s,
y.short_cty_s, y.st_s, y.bus_phn_s, x.d_mat_s,
x.d_lsd_s, x.trm_l, x.tot_org_d,
x.tot_org_d - x.acl_depr_d as rem_net_inv,
(select rgr.nam_long_s
from rgr
inner join vrcx_grt on rgr.grn_s = vrcx_grt.grn_s) as Client_Name,
n.bus_phn_s,n.hom_phn_s,o.field19_s, o.field20_s
from
rls x, vrcx_les y, req z, rlr k, ral n, rug o, rgr m, vrcx_grt p
where
x.les_s = y.les_s
and x.LSE_S = z.LSE_S
and z.dlr_s = k.dlr_s
and x.les_s = n.les_s
and x.lse_s = o.lse_s
and m.GRN_S = p.GRN_S
and x.por_s = ' 1'
and x.act_s in ('AACQ', 'AASM', 'AEXM', 'ANOR', 'ARBK', 'ARED', 'AREN', 'AXFR')
and x.d_mat_s > to_date ('07/01/2018', 'MM/DD/YYYY')
order by
x.D_MAT_S, y.les_s
您已经使用老式的联接语法编写了查询,这使得它相当不可读。改写为:
select distinct x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s,y.les_s,
y.client_s_name_s,z.DLR_S,k.nam_long_s,z.ID_S,z.res_d, y.short_add_s,
y.short_cty_s, y.st_s, y.bus_phn_s, x.d_mat_s,
x.d_lsd_s, x.trm_l, x.tot_org_d, x.tot_org_d - x.acl_depr_d as rem_net_inv,
m.nam_long_s as Client_Name,
n.bus_phn_s,n.hom_phn_s,o.field19_s, o.field20_s
from rls x
inner join vrcx_les y on x.les_s = y.les_s
inner join req z on x.LSE_S = z.LSE_S
inner join rlr k on z.dlr_s = k.dlr_s
inner join ral n on x.les_s = n.les_s
inner join rug o on x.lse_s = o.lse_s
inner join rgr m on m.les_s = x.les_s
inner join vrcx_grt p on m.grn_s = p.grn_s
where
and x.por_s = ' 1'
and x.act_s in ('AACQ', 'AASM', 'AEXM', 'ANOR', 'ARBK', 'ARED', 'AREN',
'AXFR')
and x.d_mat_s > to_date ('07/01/2018', 'MM/DD/YYYY')
order by x.D_MAT_S, y.les_s
在这里,p vrcx_grt除了充当现有检查之外似乎没有任何作用。你可以这样写:
select distinct x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s,y.les_s,
y.client_s_name_s,z.DLR_S,k.nam_long_s,z.ID_S,z.res_d, y.short_add_s,
y.short_cty_s, y.st_s, y.bus_phn_s, x.d_mat_s,
x.d_lsd_s, x.trm_l, x.tot_org_d, x.tot_org_d - x.acl_depr_d as rem_net_inv,
m.nam_long_s as Client_Name,
n.bus_phn_s,n.hom_phn_s,o.field19_s, o.field20_s
from rls x
inner join vrcx_les y on x.les_s = y.les_s
inner join req z on x.LSE_S = z.LSE_S
inner join rlr k on z.dlr_s = k.dlr_s
inner join ral n on x.les_s = n.les_s
inner join rug o on x.lse_s = o.lse_s
inner join rgr m on m.les_s = x.les_s
where
and x.por_s = ' 1'
and x.act_s in ('AACQ', 'AASM', 'AEXM', 'ANOR', 'ARBK', 'ARED', 'AREN',
'AXFR')
and x.d_mat_s > to_date ('07/01/2018', 'MM/DD/YYYY')
and EXISTS (select * from vrcx_grt p where m.grn_s = p.grn_s)
order by x.D_MAT_S, y.les_s
注意:OOPS。重写查询后,我注意到m\p和其他表之间没有任何关系。如果你纠正了,一切都会好起来的
编辑:根据您的信息编辑查询。您使用老式的联接语法编写了查询,这使得查询非常不可读。改写为:
select distinct x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s,y.les_s,
y.client_s_name_s,z.DLR_S,k.nam_long_s,z.ID_S,z.res_d, y.short_add_s,
y.short_cty_s, y.st_s, y.bus_phn_s, x.d_mat_s,
x.d_lsd_s, x.trm_l, x.tot_org_d, x.tot_org_d - x.acl_depr_d as rem_net_inv,
m.nam_long_s as Client_Name,
n.bus_phn_s,n.hom_phn_s,o.field19_s, o.field20_s
from rls x
inner join vrcx_les y on x.les_s = y.les_s
inner join req z on x.LSE_S = z.LSE_S
inner join rlr k on z.dlr_s = k.dlr_s
inner join ral n on x.les_s = n.les_s
inner join rug o on x.lse_s = o.lse_s
inner join rgr m on m.les_s = x.les_s
inner join vrcx_grt p on m.grn_s = p.grn_s
where
and x.por_s = ' 1'
and x.act_s in ('AACQ', 'AASM', 'AEXM', 'ANOR', 'ARBK', 'ARED', 'AREN',
'AXFR')
and x.d_mat_s > to_date ('07/01/2018', 'MM/DD/YYYY')
order by x.D_MAT_S, y.les_s
在这里,p vrcx_grt除了充当现有检查之外似乎没有任何作用。你可以这样写:
select distinct x.por_s, x.com_s, x.reg_s, x.off_s, x.lse_s,y.les_s,
y.client_s_name_s,z.DLR_S,k.nam_long_s,z.ID_S,z.res_d, y.short_add_s,
y.short_cty_s, y.st_s, y.bus_phn_s, x.d_mat_s,
x.d_lsd_s, x.trm_l, x.tot_org_d, x.tot_org_d - x.acl_depr_d as rem_net_inv,
m.nam_long_s as Client_Name,
n.bus_phn_s,n.hom_phn_s,o.field19_s, o.field20_s
from rls x
inner join vrcx_les y on x.les_s = y.les_s
inner join req z on x.LSE_S = z.LSE_S
inner join rlr k on z.dlr_s = k.dlr_s
inner join ral n on x.les_s = n.les_s
inner join rug o on x.lse_s = o.lse_s
inner join rgr m on m.les_s = x.les_s
where
and x.por_s = ' 1'
and x.act_s in ('AACQ', 'AASM', 'AEXM', 'ANOR', 'ARBK', 'ARED', 'AREN',
'AXFR')
and x.d_mat_s > to_date ('07/01/2018', 'MM/DD/YYYY')
and EXISTS (select * from vrcx_grt p where m.grn_s = p.grn_s)
order by x.D_MAT_S, y.les_s
注意:OOPS。重写查询后,我注意到m\p和其他表之间没有任何关系。如果你纠正了,一切都会好起来的
编辑:根据您的信息编辑查询。运行select rgr.nam_long_s from rgr internal join vrcx_grt on rgr.grn_s=vrcx_grt.grn_s并查看返回的行数。如果答案不总是1,则会出现该错误。它是否应该与查询的其余部分相关联,如果是,那么它是否总是返回一行?从rgr内部连接中选择rgr.nam_long_s在rgr.grn_s=vrcx_grt.grn_s作为客户端名称是奇数。它总是返回一行吗?@AlexPoole它返回的行不止一行。需要采取什么措施来避免这种情况?我们不知道您的数据。我想应该涉及到一些关联——这两个表之间以及主查询中的8个表之间的一些链接。子查询命中的表与主查询中的表相同,这对吗?我不知道为什么您不只是从主查询中的rgr m中获取名称-您真的需要子查询吗?i、 e.如果你用m.name_long_s作为Client_name替换整个子查询表达式,你得到你想要的结果了吗?@AlexPoole我尝试用m.name_long_s替换整个子查询,但是它会带来所有不需要的数据,而不管我在Where子句中使用的条件和m.GRN_s=p.GRN_s。当我不包括这些时查询中的列,我将获得正确的结果。但我也需要在提取中获取该列数据。运行select rgr.nam_long_s from rgr internal join vrcx_grt on rgr.grn_s=vrcx_grt.grn_s并查看返回的行数。如果答案不总是1,则会出现该错误。它是否应该与查询的其余部分相关联,如果是,那么它是否总是返回一行?从rgr内部连接中选择rgr.nam_long_s在rgr.grn_s=vrcx_grt.grn_s作为客户端名称是奇数。它总是返回一行吗?@AlexPoole它返回的行不止一行。需要采取什么措施来避免这种情况?我们不知道您的数据。我想应该涉及到一些关联——这两个表之间以及主查询中的8个表之间的一些链接。子查询命中的表与主查询中的表相同,这对吗?我不知道为什么您不只是从主查询中的rgr m中获取名称-您真的需要子查询吗?i、 e.如果你用m.name_long_s作为Client_name替换整个子查询表达式,你得到你想要的结果了吗?@AlexPoole我尝试用m.name_long_s替换整个子查询,但是它会带来所有不需要的数据,而不管我在Where子句中使用的条件和m.GRN_s=p.GRN_s。当我不包括这些时查询中的列,我将获得正确的结果。但我也需要在摘录中获取该列数据。当我试图执行上述查询时,它说p.GRN_s是无效标识符。但这是一个有效的语句。@RaviRock,不,我没有从查询中删除它,但正如我在底部指出的,m和p与其他表没有任何关系。m与其他人的关系如何?m和p与rls、ral、vrcx_les相关,其中一列与它们匹配。如果我删除m和p的这两个连接条件,我将无法在中获取m.nam_long_s的列数据extract@RaviRock,请告诉我,rgr m如何与任何表关联?除了当我试图执行上面的查询时,它说p.GRN_s是无效的标识符。但这是一个有效的语句。@RaviRock,不,我没有从查询中删除它,但正如我在底部指出的,m和p与其他表没有任何关系。m与其他人的关系如何?m和p与rls、ral、vrcx_les相关,其中一列与它们匹配。如果我删除m和p的这两个连接条件,我将无法在中获取m.nam_long_s的列数据extract@RaviRock,请告诉我,rgr m如何与任何表关联?除了p