【问题标题】:Can anyone spot the error with the WITH() clause?谁能发现 WITH() 子句的错误?
【发布时间】:2018-10-24 16:40:41
【问题描述】:

SQL 无法识别我在主 SELECT 语句中调用的变量。我调用的变量是execs.block_order_quantity。我在查询开头的WITH 语句中命名了这个变量。代码如下,错误附在图片中。我已经运行了没有WITH execs as( 部分的select 语句,它工作得很好。

WITH execs as(SELECT th.exec_id, SUM(eha.tck_ord_qty) as "BLOCK_ORDER_QUANTITY"  
FROM t1 eha
join t2 th
on eha.exec_id = th.exec_id
where th.trdng_desk_sname IN ('NAME')
and th.trd_dt between to_date('201840101', 'YYYYMMDD')
and to_date ('20140301', 'YYYYMMDD')
and exists
    (SELECT 1
     FROM t2t eth
     WHERE eth.TRD_ID = eha.TRD_ID
     AND eth.trd_stat_cd = 'EX')
group by th.exec_id)
SELECT DISTINCT
th.trd_dt as "TRADE_DATE",
eah.ord_cap_qty as "CAP_AMOUNT",
execs.block_order_quantity as "BLOCK_ORDER_QUANTITY", 
eah.alloc_ovrrd_rsn_cd as "ALLOC_OVRRD_RSN_CD",
  CASE   --create allocation case id 
        WHEN(eh.manl_alloc_ind = 'Y'
            OR NVL (eah.trdr_drct_qty, 0) > 0
            OR NVL (eah.trdr_drct_wgt_ratio, 0) > 0)
        THEN
            'Y'
        ELSE
            'N'
      END 
        AS "ALLOCATION_OVRRD_IND",
      CASE
        WHEN (eh.manl_alloc_ind = 'Y'
             OR NVL (eah.trdr_drct_qty, 0) > 0
             OR NVL (eah.trdr_drct_wgt_ratio, 0) > 0)
        THEN
          TH.EXEC_TMSTMP
        ELSE
          NULL
       END
         AS "ALLOCATION_OVRRD_TIMESTAMP",
eah.alloc_adj_assets_rt_curr_amt as "FUND_ADJ_NET_ASSETS",
eah.as_alloc_exec_hld_qty as "FUND_HOLDINGS_QUANTITY",
th.as_trd_iv_lname as "SECURITY_NAME",
th.as_trd_fmr_ticker_symb_cd as "TICKER",
   CASE  
      WHEN NVL(th.limit_prc_amt, 0) > 0 THEN 'LIMIT' ELSE NULL END
          AS "FUND_ORDER_TYPE"
from t1 eah
join t3 eh
on eah.exec_id = eah.exec_id
join t2 th
on th.trd_id = eah.trd_id
join t4 tk
on tk.tck_id = eah.tck_id
join t5 pm
on eah.pm_person_id_src = pm.person_id_src
where th.trdng_desk_sname IN('NAME')
and th.trd_dt between to_date('20140101', 'YYYYMMDD')
and to_date ('20140301', 'YYYYMMDD')
and rownum < 15

【问题讨论】:

  • 你必须引用下面from子句中的公用表表达式select
  • @Damien_The_Unbeliever -- from 子句中的公用表表达式是什么意思?通过将其加入某事?
  • @IzzyFischer - 公用表表达式 (CTE) WITH 子句。您的主要查询引用了execs.block_order_quantity,但您没有在from/join 子句中包含ececs。您需要指定该联接应如何工作。你确定你真的想要那个 CTE - 它似乎在重复主查询的部分,所以也许你真的想要一个分析总和?

标签: sql oracle common-table-expression


【解决方案1】:

您需要在主查询中加入 execs 公用表表达式 (CTE) 名称,例如:

...
from t1 eah
join t3 eh
on eah.exec_id = eah.exec_id
join t2 th
on th.trd_id = eah.trd_id
join execs
on execs.exec_id = eh.exec_id
join t4 tk
...

我不确定您是否真的想要一个 CTE,看起来您可以在主查询中进行聚合,因为您正在引用相同的表;但我可能缺少一些东西,比如后来加入的重复项。

顺便说一句,那里的第一个on 子句看起来不对,因为双方都引用同一张表中的同一列;所以应该是:

...
from t1 eah
join t3 eh
on eh.exec_id = eah.exec_id
join t2 th

拥有distinct 有时表明存在不正确的连接,并且您想要抑制不应该存在的重复项,因此一旦连接条件固定,您可能不需要它;如果以前给出错误的结果,这可能也允许简单的聚合工作。 (或者可能还有其他不合适的原因。)

此外,where rownum &lt; 15 将给出一组不确定的行,因为您在应用该过滤器之前没有对结果集进行排序。

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2022-09-25
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多