将T-SQL左连接子查询转换为MS-Access

时间:2012-07-27 02:40:09

标签: sql-server tsql ms-access jet data-conversion

我需要帮助将此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 个答案:

答案 0 :(得分:2)

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

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

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

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

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

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

答案 1 :(得分:1)

问题在于倒数第二行:

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

Access查询设计器的设计视图无法使用该子句的ON部分中的函数。您必须删除RTRIM

答案 2 :(得分:0)

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

答案 3 :(得分:0)

访问是括号快乐。将每个连接表达式括在括号中,ON子句本身和每对表。

您不能使用CONVERT,NOLOCK或CASE。