简体   繁体   中英

ORA-01427: single-row subquery returns more than one row while using select

I guess the above question has been asked a couple of times... I am still not able to implement it in my query.. in the following query there is a service table of couple of employees whose employee id is present in temp table .... each employee might have worked in different places.. hence there may be more than one record for each employee.. i am trying to find out details of the last posting ... but at the third line i get the mentioned error.. in case I use "IN" instead of equal to .. I get multiple records for each employee id

SELECT
    s.employee_id,
    s.from,
    s.to
FROM 
    service s, temp t
WHERE 
    t.employee_id = s.employee_id
    AND s.postnumber = (SELECT max(s1.postnumber)
                        FROM service s1, temp t1
                        WHERE t1.employee_id = s.employee_id)
ORDER BY 
    t.employee_id;

you have a wrong where condition in your subquery: t1.employee_id=s.employee_id

and try distinct in subquery.

try this one:

select s.employee_id,s.from,s.to from service s,temp t
where t.employee_id=s.employee_id
and s.postnumber = (select distinct max(s1.postnumber) from service s1,temp t1 where     t1.employee_id=s1.employee_id)
order by t.employee_id;

Can you please tell me that what is the point of joining table 'temp' when you are not using any of its columns in the SELECT clause.

Try putting that in an EXISTS clause.

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