sqloracle-databasenvl

nvl function with in clause


I have a problem with NVL function.I can use my in clause properyl but when i add nvl it gives error.

My query like this:

SELECT  ci.date, 
 SUM (cid.salary) amount, SUM (cid.slary_gross) gross_amount
    FROM students ci,
         orders cid           
 WHERE 
 ci.id IN  NVL((select id from sysadm.students
 where status = 'IN' AND student_id= 24514 ORDER BY id
 FETCH FIRST 3 ROWS ONLY), ci.id ) 

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

İf I delete nvl function and just write like below it is running.How can i use nvl with in clause?

ci.id IN (select id from sysadm.students where status = 'IN' AND student_id= 24514 ORDER BY id FETCH FIRST 3 ROWS ONLY)


Solution

  • I doubt the id is NULL in the subquery. SO I would suggest:

    with s3 as (
           select s.id
           from sysadm.students s.
           where s.status = 'IN' and s.student_id = 24514
           ORDER BY id
           FETCH FIRST 3 ROWS ONLY
          )
    select . . .
    where ci.id in (select id from s3) or
          not exists (select 1 from s3);
    

    This checks if your id is in the list -- or that the list is empty.