ORACLE ALIAS在WHERE子句子查询中

时间:2013-06-03 06:51:46

标签: sql oracle

这是我的sql代码。

我使用了子查询和lable's it。但是当我要从该子查询调用列时,它显示为无效标识符

SELECT   TSR.Merchant_id, denomination, SUM (no_of_cards)
    FROM sales_details
   WHERE invoice_id IN (
            SELECT invoice_id
              FROM sales_header TSR
             WHERE entered_by = (
                      (SELECT account_code
                         FROM bk_dsr_account_codes
                        WHERE user_name = 'C'
                          AND PASSWORD = 'D'
                          AND mobile_no = '8994035090213391259'))
               AND entered_date BETWEEN (TO_DATE ('2013/04/01', 'yyyy/mm/dd')
                                        )
                                    AND (TO_DATE ('2013/06/30', 'yyyy/mm/dd')
                                        ))
GROUP BY denomination

错误如下

ORA-00904: "TSR"."MERCHANT_ID": invalid identifier

请帮我解决这个问题 我还需要在最终的sql视图中调用子查询的列

2 个答案:

答案 0 :(得分:3)

您只能使用FROM子句中的列。所以,你的查询应该是这样的:

SELECT TSR.Merchant_id, SD.denomination, SUM (SD.no_of_cards)
  FROM sales_details SD
  JOIN sales_header TSR on SD.invoice_od = TSR.invoice_id
 WHERE entered_by = (
                  (SELECT account_code
                     FROM bk_dsr_account_codes
                    WHERE user_name = 'C'
                      AND PASSWORD = 'D'
                      AND mobile_no = '8994035090213391259'))
           AND entered_date BETWEEN (TO_DATE ('2013/04/01', 'yyyy/mm/dd')
                                    )
                                AND (TO_DATE ('2013/06/30', 'yyyy/mm/dd')
                                    )
GROUP BY SD.denomination

答案 1 :(得分:1)

select * from table1, table2 where table1.userid=x and table2.sdsd=x

select * from table1 t1, table2 t2 where t1.userid=x and t2.sdsd=x

同样输出这两个......

试试这个

SELECT   TSR.Merchant_id, denomination, SUM (no_of_cards)
    FROM sales_details,TSR
   WHERE invoice_id IN (
            SELECT invoice_id
              FROM sales_header TSR
             WHERE entered_by = (
                      (SELECT account_code
                         FROM bk_dsr_account_codes
                        WHERE user_name = 'C'
                          AND PASSWORD = 'D'
                          AND mobile_no = '8994035090213391259'))
               AND entered_date BETWEEN (TO_DATE ('2013/04/01', 'yyyy/mm/dd')
                                        )
                                    AND (TO_DATE ('2013/06/30', 'yyyy/mm/dd')
                                        ))
GROUP BY denomination