希望修改或更正SQL查询

时间:2015-07-08 02:30:50

标签: sql oracle

当我运行此查询时:

select sm.stylename, 
(select fieldvalue from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Name') as "VENDOR NAME",
(select fieldvalue from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Number') as "VENDOR NUMBER",
(select fieldvalue from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Address') as "VENDOR ADDRESS",
(select fieldvalue from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Name') as "FACTORY NAME",
(select fieldvalue from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Address') as "FACTORY ADDRESS"
from stylemaster sm order by sm.stylename;

我收到错误消息:

"ORA-01427: single-row subquery returns more than one row
01427. 00000-"single-row subquery returns more than one row"

您能帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的一个子查询返回多个值。您可以通过添加where rownum = 1或聚合函数来修复症状:

select sm.stylename, 
       (select max(fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Name') as "VENDOR NAME",
       (select max(fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Number') as "VENDOR NUMBER",
       (select max(fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Address') as "VENDOR ADDRESS",
       (select max(fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Name') as "FACTORY NAME",
       (select max(fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Address') as "FACTORY ADDRESS"
from stylemaster sm
order by sm.stylename;

这将解决问题,但您可能想要调查重复项开始时的原因。

编辑:

如果max()对您的数据类型无效,请使用rownum = 1

select sm.stylename, 
       (select (fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Name' and rownum = 1) as "VENDOR NAME",
       (select (fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Number' and rownum = 1) as "VENDOR NUMBER",
       (select (fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Vendor Address' and rownum = 1) as "VENDOR ADDRESS",
       (select (fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Name' and rownum = 1) as "FACTORY NAME",
       (select (fieldvalue) from auxiliaryfield af where af.stylename = sm.stylename and fieldlabel = 'Factory Address' and rownum = 1) as "FACTORY ADDRESS"
from stylemaster sm
order by sm.stylename;