1

一段时间以来,我有这个已经工作了一个月的 SQL 块:

SELECT 
    F.[BRANCH] AS REGION
    , F.[ACCOUNT]
    , F.[EFFECTIVE]
    , F.[POLICY]
    , F.[DESC2]
    , F.[DESC4]
    , F.[GROSS]
    , F.[COMM]
    , F.[GST ON COMM]
    , F.[ENTRYDATE]
    , I.[TYPE] 
FROM (
    (SELECT [POLP - Detailed Debtors Import].* 
     FROM [POLP - Detailed Debtors Import]  
     WHERE [POLP - Detailed Debtors Import].[EFFECTIVE] <= DateValue("30/05/2012")  
        AND [POLP - Detailed Debtors Import].[GROSS] >= 0  
        AND [POLP - Detailed Debtors Import].[DESC4] IN ('RNWL', 'NEWB', 'EP')  
        AND [POLP - Detailed Debtors Import].[DUEYR] = 2012) AS F 
        INNER JOIN (SELECT IPM_ACCTXREF.* FROM IPM_ACCTXREF  
                        WHERE IPM_ACCTXREF.TYPE 
                            IN (SELECT [Evolve - Account Types].[Account Types] 
                                FROM [Evolve - Account Types])
                        ) AS I ON F.[ACCOUNT] = I.[ACCOUNT]
    ) 
    LEFT JOIN (SELECT  [POLP - Detailed Debtors Import].* 
                  FROM [POLP - Detailed Debtors Import]  
                  WHERE [POLP - Detailed Debtors Import].[DESC4] IN ('CS', 'CANC', 'JL')
                  ) AS S ON F.[POLICY] = S.[POLICY] 
WHERE S.[POLICY] IS NULL

然后我想对 DateValue 参数进行更改(就像将 5 更改为 6 一样简单)。

那么问题是我重复了三次“JOIN 操作中的语法错误”。单击它们中的每一个,查询然后显示结果表,但带有“#Name?” 填充在每个字段中。

不知道这里发生了什么......

4

2 回答 2

0

将查询分解为子查询(通过所有讨厌的嵌套SELECT语句)解决了这个问题。不知道首先是什么原因(它确实曾经起作用)。

于 2012-10-22T23:52:20.377 回答
0

您可能应该重写查询,明确您需要的字段,而不仅仅是使用*.

SELECT F.REGION,
       F.ACCOUNT,
       F.EFFECTIVE,
       F.POLICY,
       F.DESC2,
       F.DESC4,
       F.GROSS,
       F.COMM,
       F.[GST ON COMM],
       F.ENTRYDATE,
       I.[TYPE]
FROM   ( (SELECT BRANCH AS REGION,
               ACCOUNT,
               EFFECTIVE,
               POLICY,
               DESC2,
               DESC4,
               GROSS,
               COMM,
               [GST ON COMM],
               ENTRYDATE
        FROM   [POLP - Detailed Debtors Import]
        WHERE  EFFECTIVE<=Datevalue("30/05/2012")
               AND GROSS>=0
               AND DESC4 IN ('RNWL', 'NEWB', 'EP')
               AND DUEYR=2012) AS F
         INNER JOIN (SELECT [TYPE],
                            ACCOUNT
                     FROM   IPM_ACCTXREF
                     WHERE  [TYPE] IN (SELECT [Account Types]
                                       FROM   [Evolve - Account Types])) AS I
           ON F.ACCOUNT=I.ACCOUNT )
       LEFT JOIN (SELECT POLICY
                  FROM   [POLP - Detailed Debtors Import]
                  WHERE  DESC4 IN ('CS', 'CANC', 'JL')) AS S
         ON F.POLICY=S.POLICY
WHERE  S.POLICY IS NULL 

我对这里的目的有点迷茫,因为主语句中实际上没有使用LEFT JOIN任何数据。SSELECT

您还使用名称TYPE作为字段名称,这是一个保留字。

于 2012-10-17T06:24:18.407 回答