从LEFT JOIN SQL获取MAX TransactionId

时间:2015-10-13 07:26:11

标签: sql sql-server

嗨我在sql server中有一个看起来像

的sql查询
SELECT
            DTST.[Id],
            FPT.[StartTime],
            --FPT.[BusinessTypeId],
            FPT.[PaymentMethodId],
            FPT.[3DSecureId],
            FPT.[ProductId],
            DPT.[Id],
            FPT.[TransactionStatusId],
            FPT.[Amount],
            DC.[Code],
            FPT.[PlayerId],
            DPL.[SourceOrigId],
            DPL.[Username],
            DPL.[FirstName],
            DPL.[LastName],
            DPL.[BrandId],
            DPL.[VIPLevelId],
            DPL.[MarketingChannelId],
            DPL.[MarketingSourceId],
            FPT.[PaymentReasonTextId],
            FPT_Ref.[PaymentReasonTextId],
            AD.Username,
            FPT.[OriginalTransactionId],
            FPT.[TransactionId],
            FPT_Ref.[OriginalTransactionId],
            FPT.[ProviderTransactionOrigId]
        FROM WarehouseMgmt.FactPaymentTrans AS FPT      
      JOIN 
      (
            SELECT TransactionId,MAX(TransactionStepId) TransactionStepId
            FROM [WarehouseMgmt].[FactPaymentTrans] 
            WHERE FactType = ''SOURCE'' 
            GROUP BY TransactionId 
      ) AS FPT_Last ON FPT.TransactionId = FPT_Last.TransactionId AND FPT.TransactionStepId = FPT_Last.TransactionStepId
        JOIN WarehouseMgmt.DimTransactionStepType AS DTST ON FPT.[TransactionStepTypeId] = DTST.[Id] 
        JOIN WarehouseMgmt.DimCurrency AS DC ON FPT.CurrencyId = DC.Id 
        JOIN WarehouseMgmt.DimPlayer AS DPL ON FPT.PlayerId = DPL.Id
        JOIN WarehouseMgmt.DimProduct AS DP ON DP.Id = FPT.ProductId
        JOIN WarehouseMgmt.DimProductType AS DPT ON DPT.Id = DP.ProductTypeId
        JOIN [WarehouseMgmt].[DimLoyaltyProgramLevel] DLPL ON DLPL.[Id]=DPL.VipLevelId
        JOIN WarehouseMgmt.DimAdmin AS AD ON AD.Id=FPT.CreatedByAdminId
        JOIN WarehouseMgmt.DimPaymentTransactionBusinessType AS DPTBT ON DPTBT.Id = FPT.BusinessTypeId
        JOIN WarehouseMgmt.DimTransactionStatus TS ON TS.Id= FPT.TransactionStatusId
        JOIN WarehouseMgmt.DimPaymentMethod DPM ON DPM.Id = FPT.[PaymentMethodId]
        JOIN WarehouseMgmt.DimTimeZone DTZ on  FPT.[TimeId] = DTZ.TimeUTCId
        LEFT JOIN [WarehouseMgmt].[FactPaymentTrans] FPT_Ref ON FPT_Ref.OriginalTransactionId = FPT.TransactionId AND FPT_Ref.FactType = ''SOURCE''       
        WHERE (FPT.FactType = ''SOURCE'') 
        AND DTZ.TimeId BETWEEN @DimStartDate AND @DimEndDate ' + @sqlFilters

有了这个查询,我有重复的交易,因为LEFT JOIN。如何改进LEFT JOIN以从LEFT JOIN中仅获取MAX TransactionId?

0 个答案:

没有答案