在子查询中明确定义了列,并在另一个子查询上加入

时间:2018-04-30 15:08:57

标签: sql oracle join plsql subquery

在加入两个子查询时,我不断收到一个含糊不清的列错误。但是我已正确定义了所有列。我想从第一个查询中获取所有数据,并在可用的地方添加一些数据。如何解决这个问题?

SELECT 
    sq2.month,
    sq1.PRIMARY_MER_NUM ,
    sq1.PRIMARY_EXT_MID ,
    sq1.MER_DBA_NAM, 
    sq1.CLG_NUM, 
    sq1.ENT_NUM, 
    sq1.ENT_NAM, 
    sq1.MER_OPN_DTE, 
    sq1.MER_CLS_DTE, 
    sq1.MER_FST_DPST_DTE,
    sq1.CLG_NUM ,
    sq1.ENT_NUM,
    sq2.gross_volume,
    sq2.transaction_count
FROM 
    (SELECT DISTINCT
        PRIMARY_MER_NUM ,
        PRIMARY_EXT_MID ,
        MER_DBA_NAM,
        CLG_NUM, 
        ENT_NUM, 
        ENT_NAM, 
        MER_OPN_DTE, 
        MER_CLS_DTE, 
        MER_FST_DPST_DTE,
        CLG_NUM ,
        ENT_NUM 
     FROM
         bi.t_mer_dim_na 
     WHERE
         CLG_NUM = 7
         AND ENT_NUM IN ('45810', '45811', '46849', '45948', '45824',
                         '46911', '45509', '46845', '48902')
    ) sq1
LEFT JOIN 
    (SELECT 
         TRUNC(BAT_REF_DTE, 'MM') AS month,
         MER_NUM,
         SUM(bat_prd_trn_dr_amt + bat_prd_trn_cr_amt) AS gross_volume, 
         SUM(bat_item_num) AS transaction_count
     FROM 
         TDS.BAT_T3 
     WHERE 
         1 = 1
         AND bat_ref_dte >= TRUNC(sysdate, 'MM') 
     GROUP BY
         TRUNC(BAT_REF_DTE, 'MM'), MER_NUM) SQ2 ON sq1.primary_mer_num = sq2.MER_NUM;

1 个答案:

答案 0 :(得分:4)

您在第一个派生表CLG_NUM中选择了ENT_NUMSQL1两次

FROM (
select DISTINCT
PRIMARY_MER_NUM ,
PRIMARY_EXT_MID ,
MER_DBA_NAM,
CLG_NUM, --1
ENT_NUM, --1
ENT_NAM, 
MER_OPN_DTE, 
MER_CLS_DTE, 
MER_FST_DPST_DTE,
CLG_NUM, --2
ENT_NUM  --2
from bi.t_mer_dim_na 

这会让您在外部选择中选择sql1.CLG_NUMsql1.ENT_NUM不明确(您也可以选择两次)