0

所以我对这个程序很陌生,无法弄清楚为什么我会收到这个错误。这是qry。

SELECT policy_no, 
       phase_code, 
       sub_phase_code, 
       Duplicate_Indicator = CASE 
                               WHEN Sum(1) <> 1 THEN 'DUPLICATE' 
                               ELSE 'NOT DUPLICATE' 
                             END 
FROM   qrypolicylistfornydefpremasset_re 
       RIGHT JOIN (SELECT TOP 100 PERCENT tblinsurance.policy_no, 
                                          tblinsurance.phase_code, 
                                          tblinsurance.sub_phase_code, 
                                          tblinsurance.prodtype, 
                                          tblinsurance.paid_to_date, 
                                          tblinsurance.val_date, 
                                          tblinsurance.issue_date, 
                                          tblinsurance.schednp, 
                                          Sum(tblinsurance.gross_annlzd_prem) AS 
                  SumOfGROSS_ANNLZD_PREM, 
                                          Sum(tblinsurance.statnetpremium)    AS 
                  SumOfStatNetPremium, 
                                          Sum(tblinsurance.netdefpremium)     AS 
                  SumOfNetDefPremium, 
                                          Sum(tblinsurance.netdefextra)       AS 
                                                           SumOfNetDefExtra, 
                                          Sum(tblinsurance.netdefpremiumadj)  AS 
                  SumOfNetDefPremiumAdj, 
                                          Sum(tblinsurance.netdefextraadj)    AS 
                  SumOfNetDefExtraAdj, 
                                          NetvsGrossInd = CASE 
                                                            WHEN 
                                          [netdefpremium] = [netdefpremiumadj] 
                                                          THEN 
                                                            'Net' 
                                                            ELSE 'Gross' 
                                                          END, 
                                          Sum(tblinsurance.amount_inforce)    AS 
                  SumOfAMOUNT_INFORCE, 
                                          Sum(tblinsurance.pua_face)          AS 
                                                           SumOfPUA_FACE, 
                                          Sum(tblinsurance.oyt_face)          AS 
                                                           SumOfOYT_FACE 
                   FROM   tblinsurance 
                   WHERE  ( ( ( tblinsurance.company_code ) = 'NL' ) 
                            AND ( ( tblinsurance.term_reason ) = 'A' ) 
                            AND ( ( tblinsurance. issue_date ) <= [val_date] ) ) 
                   GROUP  BY tblinsurance.policy_no, 
                             tblinsurance.phase_code, 
                             tblinsurance.sub_phase_code, 
                             tblinsurance.prodtype, 
                             tblinsurance.paid_to_date, 
                             tblinsurance.val_date, 
                             tblinsurance.issue_date, 
                             tblinsurance.schednp, 
                             CASE 
                               WHEN [netdefpremium] = [netdefpremiumadj] THEN 
                               'Net' 
                               ELSE 'Gross' 
                             END 
                   HAVING (( ( Sum(tblinsurance.netdefpremium) ) <> 0 )))Work 
               ON policy_no = qrypolicylistfornydefpremasset_re.policy_no 
                  AND phase_code = qrypolicylistfornydefpremasset_re.phase_code 
                  AND sub_phase_code = 
qrypolicylistfornydefpremasset_re.sub_phase_code 
GROUP  BY policy_no, 
          phase_code, 
          sub_phase_code 

我在字段 POLICY_NO、PHASE_CODE、SUB_PHASE_CODE 上得到模棱两可的列名,但仅在最后的 On 和 GROUP by 以及开头的 SELECT 中。

4

1 回答 1

4

好吧,你在这两个领域都有这些领域

qryPolicyListforNYDefPRemAsset_Re

在你的子查询中Work

所以在它们前面加上正确的表(或者更好的是,使用别名)。

Work我以in为前缀,SELECT并且我可能对 and 有误GROUP BY(也许你需要,别名):我就像 Sql,你的查询对我来说太模棱两可了;)JOINSELECTGROUP BYqqryPolicyListforNYDefPRemAsset_Re

SELECT 
    Work.POLICY_NO, 
    Work.PHASE_CODE, 
    Work.SUB_PHASE_CODE,
    Duplicate_Indicator = case when 
    SUM(1) <> 1 then 'DUPLICATE'
    else 'NOT DUPLICATE'
    end 
    FROM 

    qryPolicyListforNYDefPRemAsset_Re q

    RIGHT JOIN 

        (SELECT top 100 percent 
        t.POLICY_NO, 
        t.PHASE_CODE, 
        t.SUB_PHASE_CODE, 
        t.ProdType, 
        t.PAID_TO_DATE, 
        t.VAL_DATE, 
        t.ISSUE_DATE, 
        t.SchedNP,
        Sum(t.GROSS_ANNLZD_PREM) AS SumOfGROSS_ANNLZD_PREM, 
        Sum(t.StatNetPremium) AS SumOfStatNetPremium, 
        Sum(t.NetDefPremium) AS SumOfNetDefPremium, 
        Sum(t.NetDefExtra) AS SumOfNetDefExtra, 
        Sum(t.NetDefPremiumAdj) AS SumOfNetDefPremiumAdj, 
        Sum(t.NetDefExtraAdj) AS SumOfNetDefExtraAdj, 
        NetvsGrossInd = Case when [NetDefPremium]=[NetDefPremiumAdj] then 'Net' Else 'Gross' END, 
        Sum(t.AMOUNT_INFORCE) AS SumOfAMOUNT_INFORCE, 
        Sum(t.PUA_FACE) AS SumOfPUA_FACE, 
        Sum(t.OYT_FACE) AS SumOfOYT_FACE
        FROM tblInsurance t
        WHERE (((t.COMPANY_CODE)='NL') AND ((t.TERM_REASON)='A') AND ((t.ISSUE_DATE)<=[VAL_DATE]))
        GROUP BY 
        t.POLICY_NO, 
        t.PHASE_CODE, 
        t.SUB_PHASE_CODE, 
        t.ProdType, 
        t.PAID_TO_DATE, 
        t.VAL_DATE, 
        t.ISSUE_DATE,
        t.SchedNP,
        Case when [NetDefPremium]=[NetDefPremiumAdj] then 'Net' Else 'Gross' END
        HAVING (((Sum(t.NetDefPremium))<>0))
        )Work


    ON 
    Work.POLICY_NO = q.POLICY_NO AND 
    Work.qPHASE_CODE= q.PHASE_CODE AND 
    Work.SUB_PHASE_CODE = q.SUB_PHASE_CODE 
    GROUP BY 
    Work.POLICY_NO, 
    Work.PHASE_CODE, 
    Work.SUB_PHASE_CODE
于 2013-05-31T13:40:17.973 回答