2

我需要帮助来将此 T-SQL 查询转换为 MS ACCESS。我得到的错误是不支持 JOIN 表达式。

更新:

我不能添加:

DDA ON TT.[描述] = DDA.AccountTypeDesc AND H.AccountNumber = DDA.AccountNumber

DDA ON TT.[描述] = DDA.AccountTypeDesc

作品。有没有办法添加第二个条件?

T-SQL 查询:

SELECT
    *
FROM
(
    SELECT
        [PesoAmount] = CASE WHEN FE.IsoCode IS NULL THEN
            LTRIM(STR(DFCF.CurrencyAmount, 20, 2))
        ELSE
            LTRIM(STR(DFCF.CurrencyAmount * FE.PhpConversionRate, 20, 2))
        END,
        DFCF.TransactionNumber,
        DFCF.AccountNumber,
        DFCF.CountryCd,
        DFCF.TransactionTypeCd,
        DFCF.Time,
        DFCF.Date,
        DFCF.TransactionStatusCd,
        DFCF.TransactionCurrencyCd,
        DFCF.BranchNumber,
        DFCF.RemitterExtPartyCd,
        DFCF.BeneficiaryExtPartyCd,
        DFCF.PostedDate,
        DFCF.AssociateNumber,
        DFCF.ExecutingPartyNumber,
        DFCF.CurrencyAmount,
        DFCF.CurrencyAmountInTxnCcy,
        DFCF.CurrencyAmountInAccountCcy,
        DFCF.SecondaryAccountKey,
        DFCF.RelatedInd,
        DFCF.ThirdPartyInd,
        DFCF.TransactionDescription,
        DFCF.SecurityName,
        DFCF.DealNumber
    FROM
        dbo.DesFactCashFlow DFCF (NOLOCK)                                            LEFT JOIN
        dbo.ForeignExchange   FE (NOLOCK) ON DFCF.TransactionCurrencyCd = FE.IsoCode
)
                           H                                                                                          LEFT JOIN
    dbo.Ctr                C (NOLOCK) ON H.PesoAmount                           = C.PesoAmountFaceValueSumInsured AND
                                         H.AccountNumber                        = C.AccountNumber                 AND
                                         H.TransactionTypeCd                    = C.TransactionType                   LEFT JOIN
    dbo.TransactionType   TT (NOLOCK) ON H.TransactionTypeCd                    = TT.Code                             LEFT JOIN
(
    SELECT
        [AccountNumber]               = DDA2.AccountNumber,
        [AccountTypeDesc]             = DDA2.AccountTypeDesc,
        [LineOfBusinessName]          = MAX(DDA2.LineOfBusinessName),
        [AccountCurrencyCode]         = MAX(DDA2.AccountCurrencyCode),
        [AccountCurrencyName]         = MAX(DDA2.AccountCurrencyName),
        [AccountRegistrationTypeDesc] = MAX(DDA2.AccountRegistrationTypeDesc),
        [AccountRegistrationName]     = MAX(DDA2.AccountRegistrationName),
        [AccountName]                 = MAX(DDA2.AccountName),
        [AlternateName]               = MAX(DDA2.AlternateName),
        [AccountOpenDate]             = MAX(DDA2.AccountOpenDate),
        [AccountCloseDate]            = MAX(DDA2.AccountCloseDate),
        [AccountStatusDesc]           = MAX(DDA2.AccountStatusDesc),
        [DormantInd]                  = MAX(DDA2.DormantInd),
        [ProductLineName]             = MAX(DDA2.ProductLineName),
        [ProductCategoryName]         = MAX(DDA2.ProductCategoryName),
        [ProductTypeName]             = MAX(DDA2.ProductTypeName),
        [ProductName]                 = MAX(DDA2.ProductName),
        [ProductNumber]               = MAX(DDA2.ProductNumber),
        [AccountTaxId]                = MAX(DDA2.AccountTaxId),
        [AccountTaxIdTypeCode]        = MAX(DDA2.AccountTaxIdTypeCode),
        [AccountTaxStateCode]         = MAX(DDA2.AccountTaxStateCode),
        [AccountPrimaryBranchName]    = MAX(DDA2.AccountPrimaryBranchName),
        [MailingAddress1]             = MAX(DDA2.MailingAddress1),
        [MailingAddress2]             = MAX(DDA2.MailingAddress2),
        [MailingCityName]             = MAX(DDA2.MailingCityName),
        [MailingStateCode]            = MAX(DDA2.MailingStateCode),
        [MailingStateName]            = MAX(DDA2.MailingStateName),
        [MailingPostalCode]           = MAX(DDA2.MailingPostalCode),
        [MailingCountryCode]          = MAX(DDA2.MailingCountryCode),
        [MailingCountryName]          = MAX(DDA2.MailingCountryName),
        [CurrencyBasedAccountInd]     = MAX(DDA2.CurrencyBasedAccountInd),
        [MaturityDate]                = MAX(DDA2.MaturityDate),
        [OriginalLoanAmount]          = MAX(DDA2.OriginalLoanAmount),
        [CollateralTypeDesc]          = MAX(DDA2.CollateralTypeDesc),
        [CollateralTypeCode]          = MAX(DDA2.CollateralTypeCode),
        [InsuredAmount]               = MAX(DDA2.InsuredAmount),
        [EmployeeInd]                 = MAX(DDA2.EmployeeInd)
    FROM
        dbo.DesDimAccount DDA2 (NOLOCK)
    GROUP BY
        DDA2.AccountNumber,
        DDA2.AccountTypeDesc
)
                         DDA          ON RTRIM(TT.[Description])                = RTRIM(DDA.AccountTypeDesc)      AND
                                         H.AccountNumber                        = DDA.AccountNumber

编辑:我用 AS 关键字替换了查询。我犯了同样的错误。MS Access 查询错误:

SELECT
    'H'                           AS [HeaderRecordIndicator],
    '1'                           AS [SupervisingAgency],
    '0' + I.InstitutionCode       AS [InstitutionCode],
    CONVERT(char(8), H.Date, 112) AS [ReportDate],
    'CTR'                         AS [ReportType],
    '21'                          AS [FormatCode],
    '1'                           AS [SubmissionType]
FROM
(((
    SELECT
        IIF(ISNULL(FE.IsoCode), FORMAT(DFCF.CurrencyAmount, "##################.00"), FORMAT(DFCF.CurrencyAmount * FE.PhpConversionRate, "##################.00")) AS [PesoAmount],
        DFCF.TransactionNumber,
        DFCF.AccountNumber,
        DFCF.CountryCd,
        DFCF.TransactionTypeCd,
        DFCF.Time,
        DFCF.Date,
        DFCF.TransactionStatusCd,
        DFCF.TransactionCurrencyCd,
        DFCF.BranchNumber,
        DFCF.RemitterExtPartyCd,
        DFCF.BeneficiaryExtPartyCd,
        DFCF.PostedDate,
        DFCF.AssociateNumber,
        DFCF.ExecutingPartyNumber,
        DFCF.CurrencyAmount,
        DFCF.CurrencyAmountInTxnCcy,
        DFCF.CurrencyAmountInAccountCcy,
        DFCF.SecondaryAccountKey,
        DFCF.RelatedInd,
        DFCF.ThirdPartyInd,
        DFCF.TransactionDescription,
        DFCF.SecurityName,
        DFCF.DealNumber
    FROM
        DesFactCashFlow DFCF                                            LEFT JOIN
        ForeignExchange   FE ON DFCF.TransactionCurrencyCd = FE.IsoCode
) AS
                       H                                                                                 LEFT JOIN
    Ctr                C ON H.PesoAmount                           = C.PesoAmountFaceValueSumInsured AND
                            H.AccountNumber                        = C.AccountNumber                 AND
                            H.TransactionTypeCd                    = C.TransactionType)                   LEFT JOIN
    TransactionType   TT ON H.TransactionTypeCd                    = TT.Code)                             LEFT JOIN
(
    SELECT
        DDA2.AccountNumber                    AS [AccountNumber],
        DDA2.AccountTypeDesc                  AS [AccountTypeDesc],
        MAX(DDA2.LineOfBusinessName)          AS [LineOfBusinessName],
        MAX(DDA2.AccountCurrencyCode)         AS [AccountCurrencyCode],
        MAX(DDA2.AccountCurrencyName)         AS [AccountCurrencyName],
        MAX(DDA2.AccountRegistrationTypeDesc) AS [AccountRegistrationTypeDesc],
        MAX(DDA2.AccountRegistrationName)     AS [AccountRegistrationName],
        MAX(DDA2.AccountName)                 AS [AccountName],
        MAX(DDA2.AlternateName)               AS [AlternateName],
        MAX(DDA2.AccountOpenDate)             AS [AccountOpenDate],
        MAX(DDA2.AccountCloseDate)            AS [AccountCloseDate],
        MAX(DDA2.AccountStatusDesc)           AS [AccountStatusDesc],
        MAX(DDA2.DormantInd)                  AS [DormantInd],
        MAX(DDA2.ProductLineName)             AS [ProductLineName],
        MAX(DDA2.ProductCategoryName)         AS [ProductCategoryName],
        MAX(DDA2.ProductTypeName)             AS [ProductTypeName],
        MAX(DDA2.ProductName)                 AS [ProductName],
        MAX(DDA2.ProductNumber)               AS [ProductNumber],
        MAX(DDA2.AccountTaxId)                AS [AccountTaxId],
        MAX(DDA2.AccountTaxIdTypeCode)        AS [AccountTaxIdTypeCode],
        MAX(DDA2.AccountTaxStateCode)         AS [AccountTaxStateCode],
        MAX(DDA2.AccountPrimaryBranchName)    AS [AccountPrimaryBranchName],
        MAX(DDA2.MailingAddress1)             AS [MailingAddress1],
        MAX(DDA2.MailingAddress2)             AS [MailingAddress2],
        MAX(DDA2.MailingCityName)             AS [MailingCityName],
        MAX(DDA2.MailingStateCode)            AS [MailingStateCode],
        MAX(DDA2.MailingStateName)            AS [MailingStateName],
        MAX(DDA2.MailingPostalCode)           AS [MailingPostalCode],
        MAX(DDA2.MailingCountryCode)          AS [MailingCountryCode],
        MAX(DDA2.MailingCountryName)          AS [MailingCountryName],
        MAX(DDA2.CurrencyBasedAccountInd)     AS [CurrencyBasedAccountInd],
        MAX(DDA2.MaturityDate)                AS [MaturityDate],
        MAX(DDA2.OriginalLoanAmount)          AS [OriginalLoanAmount],
        MAX(DDA2.CollateralTypeDesc)          AS [CollateralTypeDesc],
        MAX(DDA2.CollateralTypeCode)          AS [CollateralTypeCode],
        MAX(DDA2.InsuredAmount)               AS [InsuredAmount],
        MAX(DDA2.EmployeeInd)                 AS [EmployeeInd]
    FROM
        DesDimAccount DDA2
    GROUP BY
        DDA2.AccountNumber,
        DDA2.AccountTypeDesc
) AS
                         DDA          ON RTRIM(TT.[Description])                = RTRIM(DDA.AccountTypeDesc)      AND
                                         H.AccountNumber                        = DDA.AccountNumber                   

这是具有相同错误的简化查询:

SELECT
    *
FROM
(((
    SELECT
        IIF(ISNULL(FE.IsoCode), FORMAT(DFCF.CurrencyAmount, "##################.00"), FORMAT(DFCF.CurrencyAmount * FE.PhpConversionRate, "##################.00")) AS [PesoAmount],
        DFCF.TransactionNumber,
        DFCF.TransactionCurrencyCd,
    FROM
        DesFactCashFlow DFCF                                            LEFT JOIN
        ForeignExchange   FE ON DFCF.TransactionCurrencyCd = FE.IsoCode
) AS
                       H                                                                                 LEFT JOIN
    Ctr                C ON H.PesoAmount                           = C.PesoAmountFaceValueSumInsured AND
                            H.AccountNumber                        = C.AccountNumber                 AND
                            H.TransactionTypeCd                    = C.TransactionType)                   LEFT JOIN
    TransactionType   TT ON H.TransactionTypeCd                    = TT.Code)                             LEFT JOIN
(
    SELECT
        DDA2.AccountNumber                    AS [AccountNumber],
        DDA2.AccountTypeDesc                  AS [AccountTypeDesc],
        MAX(DDA2.LineOfBusinessName)          AS [LineOfBusinessName],
    FROM
        DesDimAccount DDA2
    GROUP BY
        DDA2.AccountNumber,
        DDA2.AccountTypeDesc
) AS
                         DDA          ON RTRIM(TT.[Description])                = RTRIM(DDA.AccountTypeDesc)      AND
                                         H.AccountNumber                        = DDA.AccountNumber                   
4

4 回答 4

2

放弃尝试SQL 文本从 T-SQL 查询转换为 Access SQL。从头开始创建新的 Access 查询并将 T-SQL 查询仅用作路线图。添加您的数据源并设置连接。查询设计器将保证您以使数据库引擎满意的方式创建连接:添加和定位具有多个连接的查询所需的括号;适用于 LEFT JOIN 的规则;等等。让设计师为您处理这些细节。

由于您上次加入的这一部分中的功能,设计师将在设计视图中窒息:

RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc)

因此,RTRIM()当您在设计视图中设置连接时,请忽略这些功能。不要担心查询不会返回正确的结果。在获得满足 db 引擎的连接后,切换到 SQL 视图并RTRIM()重新添加函数。

正确设置连接后,将字段表达式添加到SELECT列表中。

此外,您可能会发现通过将子查询分解为单独的已保存查询来更容易管理您的复杂查询 --- 然后在主查询中按名称引用这些查询,就像您使用表源一样。

于 2012-07-27T07:16:23.607 回答
1

问题在于倒数第二行:

  DDA ON RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc) AND

Access 查询设计器的设计视图不能与ON子句部分中的函数一起使用。您必须删除RTRIM.

于 2012-07-27T06:46:05.777 回答
0

您在系统中使用的是哪个版本的 MSAccess?我刚刚在 2007 版本中尝试过,并且 RTRIM 正在工作。

于 2012-07-27T06:53:39.750 回答
0

访问是愉快的括号。将每个连接表达式、ON 子句本身和每对表都包裹在括号中。

您不能使用 CONVERT、NOLOCK 或 CASE。

于 2012-07-27T07:51:27.797 回答