谁能发现 WITH() 子句的错误?
Can anyone spot the error with the WITH() clause?
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
您需要在主查询中加入 execs
通用 table 表达式 (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,看起来您可以在主查询中进行聚合,因为您引用的是相同的 tables;但可能有一些我遗漏的东西,比如后来的加入引入的重复项。
顺便说一下,那里的第一个 on
子句看起来不对,因为两边都引用了同一个 table 中的同一列;所以应该是:
...
from t1 eah
join t3 eh
on eh.exec_id = eah.exec_id
join t2 th
distinct
有时表示连接不正确,并且存在您想要抑制的重复项,但实际上不应该存在,因此一旦连接条件固定,您可能不需要它;如果它之前给出了错误的结果,那么它也可能允许简单的聚合工作。 (或者可能还有其他一些不合适的原因。)
此外,where rownum < 15
将给出一组不确定的行,因为您在应用该过滤器之前没有对结果集进行排序。
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
您需要在主查询中加入 execs
通用 table 表达式 (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,看起来您可以在主查询中进行聚合,因为您引用的是相同的 tables;但可能有一些我遗漏的东西,比如后来的加入引入的重复项。
顺便说一下,那里的第一个 on
子句看起来不对,因为两边都引用了同一个 table 中的同一列;所以应该是:
...
from t1 eah
join t3 eh
on eh.exec_id = eah.exec_id
join t2 th
distinct
有时表示连接不正确,并且存在您想要抑制的重复项,但实际上不应该存在,因此一旦连接条件固定,您可能不需要它;如果它之前给出了错误的结果,那么它也可能允许简单的聚合工作。 (或者可能还有其他一些不合适的原因。)
此外,where rownum < 15
将给出一组不确定的行,因为您在应用该过滤器之前没有对结果集进行排序。