teradata,case-into-where子句

axkjgtzd  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(358)

在这件事上我需要帮助。我的条件不被接受。。。我知道where子句的情况很棘手,所以我想你可以放我出去。。。我在子查询中创建了一个字段,其中有一个over partition by,然后我将其放入主select。。。然后我需要应用下面的过滤器。。。它返回了一个错误,说b不存在,如果我只写b.cc=1,那么它会给我一个结果。。。有什么想法吗?提前谢谢

SELECT 
    B.*

FROM

    (
    SELECT 
        A.*,
        (Count(A.COD_ABI) Over (PARTITION BY A.COD_ABI, A.COD_KTO)) AS CC   

    FROM (
    SELECT DISTINCT
        T2.COD_PRODT_SALDO, 
        T2.COD_RESID_NPE, 
        T2.COD_DIVISA_UIC, 
        T2.COD_ABI, 
        T2.COD_NDG, 
        T2.COD_KTO, 
        T2.COD_PAESE_UIC_NPE, 
        T2.DAT_SCA, 
        T2.DAT_ACC, 
        T2.DAT_EST
    FROM
        (
        SELECT 
            T1.COD_PRODT_SALDO
           ,T1.COD_RESID_NPE
           ,T1.COD_DIVISA_UIC
           ,T1.COD_ABI
           ,T1.COD_NDG
           ,'00753' ||T1.COD_PRODT_SALDO||T1.COD_CNTRT_SALDO AS COD_KTO
           ,T1.COD_CONTRATTO_SAL
           ,T1.COD_RIFER_ANNO
           ,T1.COD_RIFER_MESE
           ,T1.COD_RIFER_ANNO || T1.COD_RIFER_MESE AS COD_RIFER
           ,T1.COD_CONTB_ETR
           ,T1.DAT_EST
           ,T1.DAT_ACC
           ,T1.DAT_SCA
           ,T1.COD_PAESE_UIC_NPE

        FROM ES777A.VA_ES_DB_ANAGR_CONTO AS T1,
             ES777A.VE_BFD_PDC AS T2
            WHERE  T1.TMS_INIZIO_VALIDITA <= T2.TMS_PDC
            AND T1.TMS_FINE_VALIDITA > T2.TMS_PDC
          AND T1.TMS_CANC_FISICA IS NULL 
          AND T1.FLG_RIFACIMENTO = 0
          AND T1.COD_ABI = T2.COD_ABI
          AND T2.NOM_VISTA='VA_ES_DB_ANAGR_CONTO'
          AND T2.NUM_PERIO_RIF = 20200131
          AND T2.COD_PERIODICITA = 'G'
          AND T1.COD_PRODT_SALDO NOT IN ('1398' , '1698') 
          AND T1.COD_PRODT_SALDO IN ('1801', '1803', '1901', '1903', '3301', '3304', '3311', '3401', '3411', '3421')
        )
        AS T2

    INNER JOIN 
        (
        SELECT
            T1.COD_ABI,
            '00753'||T1.COD_PRODT_SALDO||T1.COD_CNTRT_SALDO AS COD_KTO,
             Max(T1.COD_RIFER_ANNO || COD_RIFER_MESE) AS MAX_COD_RIFER

        FROM ES777A.VA_ES_DB_ANAGR_CONTO AS T1,
             ES777A.VE_BFD_PDC AS T2
        WHERE  
            T1.TMS_INIZIO_VALIDITA <= T2.TMS_PDC
            AND T1.TMS_FINE_VALIDITA > T2.TMS_PDC
            AND T1.TMS_CANC_FISICA IS NULL 
            AND T1.FLG_RIFACIMENTO = 0
            AND T1.COD_ABI = T2.COD_ABI
            AND T2.NOM_VISTA = 'VA_ES_DB_ANAGR_CONTO'
            AND T2.NUM_PERIO_RIF = 20200131
            AND T2.COD_PERIODICITA = 'G'
            AND T1.COD_PRODT_SALDO NOT IN ('1398' , '1698')
            AND T1.COD_PRODT_SALDO IN ('1801', '1803', '1901', '1903', '3301', '3304', '3311', '3401', '3411', '3421') 
        GROUP BY T1.COD_PRODT_SALDO,T1.COD_ABI,T1.COD_CNTRT_SALDO 
        )

        AS T1
    ON ( T2.COD_ABI = T1.COD_ABI AND T2.COD_KTO = T1.COD_KTO AND T2.COD_RIFER = T1.MAX_COD_RIFER )
    WHERE
          ( T2.DAT_EST > '2019-11-02' OR T2.DAT_EST IS NULL ) -- -90GG 
      AND ( T2.DAT_SCA > '2019-11-02' OR T2.DAT_SCA IS NULL ) -- -90GG

      )
      A

    )
    B

    WHERE b.cc = 

                WHEN (B.CC > 1 AND B.DAT_EST IS NOT NULL) THEN 1
                WHEN (B.CC > 1 AND B.DAT_EST IS NULL) THEN 0
                WHEN (B.CC = 1) THEN 1 ELSE 0
            END
9rnv2umw

9rnv2umw1#

这回答了问题的原始版本。
在teradata中,可以将逻辑简化为:

SELECT ...,
       Count(A.COD_ABI) Over (PARTITION BY A.COD_ABI, A.COD_KTO) AS CC  
FROM A 
QUALIFY CC > 1 AND DATE_EST IS NOT NULL;

所有子查询都是不必要的

相关问题