当计数返回零时,计数返回零行

时间:2017-03-08 20:42:27

标签: sql database teradata data-warehouse

SELECT             
        P.P_DESC AS P,     
        D.PD_NAME AS D,     
        D.GPE_NBR AS GPE,     
        D.GPE_GEN_NM,
        D.GPE_2_ ,        
        D.GPE_4c , 
        SUBSTR (TRIM(C.FILL_DATE__ID),1,6),      
        C.fill_Date__ID,   
        Prod.PD_DESC ,
        DMC.M_A_NBR,
        DMC.MAD_NBR ,
        DMC.FT_NAME,
        DMC.LA_NAME,
        DDB.DATE_DATE, 
        CAST((CURRENT_DATE - DDB.DATE_DATE)/365.25  as Int) M_AGE_TODAY,
        RXDOC.PB_FT_NAME || ' ' || RXDOC.PB_LA_NAME ,
        RXDOC.PB_ID,
        DT.D_TYPE_DESC ,
        CASE WHEN SDL.GEPE IS NOT NULL THEN 'Y' ELSE 'N' END AS FLAG,
        COUNT (C.PHR_C_ID) AS CCount, 
        SUM (C.AMT_PAID) AS Spend
    FROM   O_PHAR_C C           
    INNER JOIN _D D ON C.D__ID=D.D__ID   
    INNER JOIN _P P               
        ON  C.P__ID = P.P__ID 
        AND C.P__ID = 00001
    INNER JOIN _M_CURR DMC ON C.PB_M_CURR_ID = DMC.M_CURR_ID
    INNER JOIN _M DM ON DM.M__ID  = DMC.M__ID 
    INNER JOIN _DATE  DDB ON DDB.DATE__ID = DM.BIRTH_DATE__ID 
    INNER JOIN _M_ELIG_CURR DMEC ON C.PB_M_ELIG_CURR_ID = DMEC.M_ELIG_CURR_ID
    LEFT OUTER JOIN BA_PROD_LAB_OWN.specialtyDList SDL
        ON D.GPE_NBR = SDL.GPE
        AND SDL.EFF_END_DATE >= CURRENT_DATE
    LEFT OUTER JOIN _PD Prod ON DMEC.PD__ID = Prod.PD__ID
    LEFT OUTER JOIN _RX_PB RXDOC ON  C.PB__ID=RXDOC.PB__ID 
    LEFT OUTER JOIN _PHAR_D_TYPE DT ON C.PHAR_D_TYPE__ID = DT.PHAR_D_TYPE__ID      
    WHERE C.fill_date__ID BETWEEN 20170201 AND 20170228
        AND C.RE_I = 'N'   
        AND C.sR__ID IN (96,13,203)   
    GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15, 16, 17, 18, 19
        HAVING COUNT(*) >= 10;

我想使用此查询返回大于或等于10的C.PHR_C_ID实例。但是,当我将select语句限制为该列时,我得到了所需的结果但是当我将所有这些列添加到它有数数'返回0.记录超过零;我认为查询有问题。

1 个答案:

答案 0 :(得分:1)

问题在于,当您添加字段和GROUP BY时,您将获得该字段的不同字段的记录计数。您可以改为切换到QUALIFY语句。这将通过不同的C.PHR_C_ID总结结果,忽略其他字段,然后返回具有count(*)> = 10的不同C.PHR_C_ID的任何字段。

SELECT             
                            P.P_DESC AS P,     
                            D.PD_NAME AS D,     
                            D.GPE_NBR AS GPE,     
                            D.GPE_GEN_NM,
                            D.GPE_2_ ,        
                            D.GPE_4c , 
                            SUBSTR (TRIM(C.FILL_DATE__ID),1,6),      
                            C.fill_Date__ID,   
                            Prod.PD_DESC ,
                            DMC.M_A_NBR,
                            DMC.MAD_NBR ,
                            DMC.FT_NAME,
                            DMC.LA_NAME,
                            DDB.DATE_DATE, 
                            CAST((CURRENT_DATE - DDB.DATE_DATE)/365.25  as Int) M_AGE_TODAY,

                            RXDOC.PB_FT_NAME || ' ' || RXDOC.PB_LA_NAME ,
                            RXDOC.PB_ID,
                            DT.D_TYPE_DESC ,
                            CASE
                                            WHEN SDL.GEPE IS NOT NULL THEN 'Y'
                                            ELSE 'N'
                            END AS FLAG,

                          COUNT(*) OVER (PARTITION BY C.PHR_C_ID) AS CCount, 

                          C.AMT_PAID AS Spend

            FROM   O_PHAR_C C           


        INNER JOIN _D D 
                            ON  C.D__ID=D.D__ID   


        INNER JOIN _P P               
            ON  C.P__ID = P.P__ID 
            AND C.P__ID = 00001





        INNER JOIN _M_CURR DMC
            ON C.PB_M_CURR_ID = DMC.M_CURR_ID

            INNER JOIN _M DM 
            ON DM.M__ID  = DMC.M__ID 

            INNER JOIN _DATE  DDB
            ON DDB.DATE__ID = DM.BIRTH_DATE__ID 

            INNER JOIN _M_ELIG_CURR DMEC
                            ON C.PB_M_ELIG_CURR_ID = DMEC.M_ELIG_CURR_ID

            LEFT OUTER JOIN BA_PROD_LAB_OWN.specialtyDList SDL
                            ON D.GPE_NBR = SDL.GPE
                            AND SDL.EFF_END_DATE >= CURRENT_DATE

            LEFT OUTER JOIN _PD Prod
                            ON DMEC.PD__ID = Prod.PD__ID

      LEFT OUTER JOIN _RX_PB RXDOC 
                            ON  C.PB__ID=RXDOC.PB__ID 

      LEFT OUTER JOIN _PHAR_D_TYPE DT
                            ON C.PHAR_D_TYPE__ID = DT.PHAR_D_TYPE__ID


                 WHERE 
                C.fill_date__ID BETWEEN 20170201 AND 20170228
                AND C.RE_I = 'N'   
                AND C.sR__ID IN (96,13,203)   
               QUALIFY COUNT(*) OVER (PARTITION BY C.PHR_C_ID) >= 10;