where子句PL / SQL查询从dual中选择的错误

时间:2013-12-09 15:37:07

标签: sql oracle

我正在进行以下查询,

SELECT 'Payment Token,Transaction ID,Create Date' 
  FROM dual 
 UNION ALL 
SELECT DISTINCT 
       pt.mw_payment_token ||','||
       t.mw_transaction_id ||','||
       t.create_date 
  FROM t_mw_payment_token pt, 
       t_mw_transaction t 
 where pt.mw_payment_token =  t.mw_payment_token 
 order by t.mw_transaction_id;

错误是:

ORA-00904: "T"."MW_TRANSACTION_ID": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 5 Column: 103

为什么列mw_transaction_id无效?

修改:

t_mw_transaction table

ENABLECREATE TABLE WFMW.T_MW_TRANSACTION 
(
  MW_PAYMENT_TOKEN VARCHAR2(40 BYTE) NOT NULL,
  CREATE_DATE DATE DEFAULT NULL, 
  MW_TRANSACTION_ID NUMBER NOT NULL,
  TRANSACTION_TYPE NUMBER(*, 0),
  CONSTRAINT XPK_T_MW_TRANSACTION PRIMARY KEY 
  (
    MW_TRANSACTION_ID 
  )
  ENABLE 
) 

mw_payment_token表

ENABLECREATE TABLE WFMW.T_MW_PAYMENT_TOKEN 
(
  MW_PAYMENT_TOKEN VARCHAR2(40 BYTE) NOT NULL 
, CREATED_BY VARCHAR2(50 BYTE) 
, MODIFIED_BY VARCHAR2(50 BYTE) 
, CREATE_DATE DATE 
, MODIFIED_DATE DATE 
, TOKENIZED_PAN VARCHAR2(20 BYTE) 
, CONSTRAINT XPKT_MW_PAYMENT_TOKEN PRIMARY KEY 
  (
    MW_PAYMENT_TOKEN 
  )
  ENABLE 
) 

6 个答案:

答案 0 :(得分:4)

您的问题是ORDER BY - 子句,因为它在整个查询中是全局的,并且在整个语句中无法识别t.mw_transaction_id
与以下相同的问题:

select 'a' from dual 
union all
select dummy from dual
order by dummy;

您可以通过为两个查询中的列提供相同的别名来解决此问题:

select 'a' some_col from dual 
union all
select dummy some_col from dual
order by some_col;

但我认为这不是你想要达到的目标。

修改
我不确定你想用你的查询实现什么,但也许这会给你想要的结果:

SELECT DISTINCT 
       pt.mw_payment_token "Payment Token",
       t.mw_transaction_id "Transaction ID",
       t.create_date "Create Date"
  FROM t_mw_payment_token pt, 
       t_mw_transaction t 
 where pt.mw_payment_token =  t.mw_payment_token 
 order by "Transaction ID";

<强> EDIT2
如果您希望输出为CSV,可以选中hereherehere。也许考虑在这里问另一个问题 我的建议是使用UTL_FILE包迭代基于我第一次编辑的游标。但到目前为止我还没有尝试过。

答案 1 :(得分:1)

您希望order by子句仅用于语句的第二部分。因此:

SELECT 'Payment Token,Transaction ID,Create Date'
  FROM dual 
 UNION ALL
SELECT * FROM
( 
  SELECT DISTINCT 
         pt.mw_payment_token ||','||
         t.mw_transaction_id ||','||
         t.create_date 
    FROM t_mw_payment_token pt, 
         t_mw_transaction t 
   where pt.mw_payment_token =  t.mw_payment_token 
   order by t.mw_transaction_id
);

答案 2 :(得分:0)

从Thorsten Kettner的回答中删除DISTINCT子句允许查询运行。

SELECT 'Payment Token,Transaction ID,Create Date'
  FROM dual 
 UNION ALL
SELECT * FROM
( 
  SELECT 
         pt.mw_payment_token ||','||
         t.mw_transaction_id ||','||
         t.create_date 
    FROM t_mw_payment_token pt, 
         t_mw_transaction t 
   where pt.mw_payment_token =  t.mw_payment_token 
   order by t.mw_transaction_id
);

答案 3 :(得分:0)

抱歉,当我写下另一个答案时,我很着急。这是一个未经测试但希望有效的解决方案。首先收集所有记录,为您的标题提供一个低排序键。然后排序:

SELECT the_string
FROM
(
  SELECT 'Payment Token,Transaction ID,Create Date' as the_string, -1 as sort_key
    FROM dual 
   UNION ALL
  SELECT DISTINCT 
           pt.mw_payment_token ||','|| t.mw_transaction_id ||','|| t.create_date as the_string,
           t.mw_transaction_id as sort_key
    FROM t_mw_payment_token pt 
    JOIN t_mw_transaction t ON pt.mw_payment_token =  t.mw_payment_token
)
ORDER BY sort_key

答案 4 :(得分:0)

按1排序 或者在两个查询中给你的列一个很好的别名(只有第一个问题)并按它排序

答案 5 :(得分:0)

以下是解决方案:

    SELECT 'Payment Token,Transaction ID,Create Date'
    FROM dual 
    UNION ALL
    SELECT 
             mw_payment_token ||','||
             mw_transaction_id ||','||
             create_date 
        FROM 
            (SELECT 
             DISTINCT
             pt.mw_payment_token,
             t.mw_transaction_id,
             t.create_date
             FROM t_mw_payment_token pt, 
             t_mw_transaction t 
             WHERE pt.mw_payment_token =  t.mw_payment_token 
             ORDER BY t.mw_transaction_id);