单行子查询在执行Select语句时在select中返回多个行

时间:2018-07-10 17:01:15

标签: 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_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

1 个答案:

答案 0 :(得分:2)

您已使用旧风格的联接语法编写了查询,这使它相当难以阅读。如何改写为:

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)除了充当“ EXISTS”检查外似乎没有任何作用。您可以这样写:

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

注意:糟糕。重写查询后,我注意到m \ p与其余表之间没有任何关系。如果您纠正的话,那应该没问题。

编辑:根据您的信息编辑查询。