返回多行的子查询

A subquery returning multple rows

我在以下查询中遇到错误..任何人都可以帮助修复此查询

select c.CHANGE_NUMBER, 
(select le.ENTRYVALUE from listentry  le
where le.ENTRYID = c.CATEGORY)as CATEGORY ,
(
            SELECT  last_name || ',' || first_name || '(' || loginid || ')'
            FROM agileuser
            WHERE id = c.ORIGINATOR
        ) initiator,(
            SELECT  last_name || ',' || first_name || '(' || loginid || ')'
            FROM agileuser
            WHERE id = c.owner
        ) coordinator, w.state,

(SELECT description
            FROM nodetable
            WHERE id = w.state
        ) workflow_status , TO_CHAR(w.LOCAL_DATE,'DD-MON-YY HH24:MI:SS')local_date,
      (select  
        (trunc(to_date(B.local_date, 'DD-MON-YY HH24:MI:SS') - to_date(w.local_date, 'DD-MON-YY HH24:MI:SS')))as timediff
        FROM WORKFLOW_PROCESS w INNER JOIN WORKFLOW_PROCESS B ON B.order_by = (w.order_by + 1) 
        where w.change_id=B.Change_id and w.change_id=9946301 )--ORDER BY w.order_by ASC 

       from change c, WORKFLOW_PROCESS w 
        where w.change_id = c.id-- order by w.ORDER_BY)
       and c.CHANGE_NUMBER='NPI001084' 
      order by w.ORDER_BY,local_date ASC 

t运行c()查询给出多条记录

 select  
     (trunc(to_date(B.local_date, 'DD-MON-YY HH24:MI:SS') - to_date(w.local_date, 'DD-MON-YY HH24:MI:SS')))as timediff
      FROM WORKFLOW_PROCESS w INNER JOIN WORKFLOW_PROCESS B ON B.order_by = (w.order_by + 1) 
      where w.change_id=B.Change_id and w.change_id=9946301 ORDER BY w.order_by ASC 

因此无法 运行 与此主查询一起使用。

真是一团糟!您真的应该考虑使用 SQL 格式化程序,或者 - 如果由于某种原因您不能 - 尝试格式化您手动编写的代码。这是不可读的。

总之:虽然子查询看起来是解决问题的好方法,但通常 - 避免它们。其中大部分都可以重写,以便您正确连接所涉及的表。

如果您选择在 SELECT 语句中使用它们,您必须确保它们中的每一个 returns 都是一个单独的值。

这 returns 两行或更多行:

SELECT (TRUNC (
             TO_DATE (b.local_date, 'DD-MON-YY HH24:MI:SS')
           - TO_DATE (w.local_date, 'DD-MON-YY HH24:MI:SS')))
          AS timediff
  FROM workflow_process w
       INNER JOIN workflow_process b ON b.order_by = (w.order_by + 1)
 WHERE     w.change_id = b.change_id
       AND w.change_id = 9946301

如何解决?我不知道,我没有你的桌子,也不知道你想要什么。简单的选项可能是:

select distinct timediff ...

select max(timediff) ...

select ...
where rownum = 1

或其他一些选项,但是 - 同样,这取决于我们不知道的事情。

完全未经测试,可能有一些拼写错误,也可能是应该是左连接的连接,并且可能是不正确的 oracle 语法,但是...

select c.CHANGE_NUMBER, 
    le.ENTRYVALUE as CATEGORY
    orig.last_name || ',' || orig.first_name || '(' || orig.loginid || ')' as initiator,
    own.last_name || ',' || own.first_name || '(' || own.loginid || ')' as coordinator,
    w.state,
    n.description as workflow_status,
    TO_CHAR(w.LOCAL_DATE,'DD-MON-YY HH24:MI:SS')local_date,
    trunc(to_date(B.local_date, 'DD-MON-YY HH24:MI:SS') - to_date(w.local_date, 'DD-MON-YY HH24:MI:SS')) as timediff
from change c
join WORKFLOW_PROCESS w on w.change_id = c.id
join listentry le on le.ENTRYID = c.CATEGORY
join agileuser as orig on orig.id = c.ORIGINATOR
join agileuser as own on own.id = c.owner
join nodetable as n on n.id = w.state
JOIN WORKFLOW_PROCESS B ON B.order_by = (w.order_by + 1) 
where c.CHANGE_NUMBER='NPI001084' 
order by w.ORDER_BY,local_date ASC