在SELECT查询Oracle DB中使用虚拟列

时间:2014-05-05 05:12:05

标签: sql oracle

我想了解如何使用在select查询中创建的虚拟变量。就像在下面的例子中我创建了RequestDate和ReceivedDate作为select查询的一部分,我想在同一范围内的第三个选择查询中使用..是否可以这样做..?请指教。

SELECT MessageId,
(  SELECT 
    xxxx from xxx where xxx
 ) AS RequestDate ,


( SELECT 
xxxx from xxx where xxx
) AS ReceivedDate ,

(
SELECT 
ReceivedDate - RequestDate from xxxx
) as DateDiff
FROM Date

1 个答案:

答案 0 :(得分:1)

SELECT MessageId,RequestDate ,ReceivedDate ,
       ReceivedDate - RequestDate as DateDiff
FROM (
       SELECT MessageId,
              (SELECT  xxxx from xxx where xxx) AS RequestDate ,
              (SELECT  xxxx from xxx where xxx) AS ReceivedDate ,
        FROM Date 
     ) As Z

注意: 您的内部查询(SELECT xxxx from xxx where xxx)(SELECT xxxx from xxx where xxx)必须为从DATE表返回的每一行返回单个值,否则您将收到错误

Oracle更新: ORA-01427 :单行子查询返回多行