Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

sql - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...