简体   繁体   中英

Want SQL query to be modified or corrected

When I am running this query:

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;

I get the error message:

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

Can you please help in resolving this issue?

One of your subqueries is returning more than one value. You can fix the symptom by adding where rownum = 1 or an aggregation function:

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;

That will get rid of the problem, but you might want to investigate why the duplicates are showing up to begin with.

EDIT:

If max() doesn't work on your data types, then use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM