SQL - 选择相应记录上列不同的行

时间:2015-09-15 18:17:48

标签: sql tsql sql-server-2012

onCreate

所以我的问题......我正在使用CDC表,这里有一些我关注的专栏......

SELECT
    RecordType = 7
    ,plc.PurchaseLogID
    ,pl.StoreID
    ,pl.UserID
    ,pl.ContractPurchaseID
    ,pl.ShoppingCartID
    ,PaymentDue = tm.tran_end_time
    ,PaymentMade = NULL
    ,pl.PurchaseWith
    ,pl.setupfee
    ,pl.FeeType
    ,pl.ProductID
    ,pl.PackageID
    ,ItemDescription = pl.PackageName
    ,CardType = NULL
    ,PaymentTransactionId = NULL
    ,PaymentStatus = NULL
    ,PaymentAmount = 
      CASE
        WHEN pl.onAccount = 1 THEN -util.ToDecimal172(pl.PurchaseAmount)
        ELSE util.ToDecimal172(pl.PurchaseAmount)
      END
    ,PaymentSalesTax = 
      CASE
        WHEN pl.onAccount = 1 THEN -util.ToDecimal172(pl.SalesTaxAmt)
        ELSE util.ToDecimal172(pl.PurchaseAmount)
      END
    ,RefundTransactionId = NULL
FROM
    [cdc].[dbo_PurchaseLog_CT] plc WITH (NOLOCK)    
    INNER JOIN [cdc].[lsn_time_mapping] tm WITH (NOLOCK)  ON plc.__$Start_lsn = tm.Start_lsn
    INNER JOIN dbo.purchaselog pl WITH (NOLOCK) ON plc.Purchaselogid = pl.purchaselogid
    INNER join StoreSet s ON pl.StoreID = s.Storeid
WHERE 
    tm.tran_end_time >= @fromDate
    AND tm.tran_end_time < @todate
    AND plc.__$operation = 4
    AND plc.DELETED IS NULL
    AND pl.RefundID IS NULL

在我的存储过程中,我只需要执行SELECT操作= 4 AND Deleted = 1,其中相应的($ operation 3,基于$ start_lsn&amp;&amp; ID)已删除= NULL。

我不确定我实现这一目标有多远...任何提示?多谢你们!

1 个答案:

答案 0 :(得分:0)

SELECT
    RecordType = 7
    ,plc.PurchaseLogID
    ,pl.StoreID
    ,pl.UserID
    ,pl.ContractPurchaseID
    ,pl.ShoppingCartID
    ,PaymentDue = tm.tran_end_time
    ,PaymentMade = NULL
    ,pl.PurchaseWith
    ,pl.setupfee
    ,pl.FeeType
    ,pl.ProductID
    ,pl.PackageID
    ,ItemDescription = pl.PackageName
    ,CardType = NULL
    ,PaymentTransactionId = NULL
    ,PaymentStatus = NULL
    ,PaymentAmount = 
      CASE
        WHEN pl.onAccount = 1 THEN -util.ToDecimal172(pl.PurchaseAmount)
        ELSE util.ToDecimal172(pl.PurchaseAmount)
      END
    ,PaymentSalesTax = 
      CASE
        WHEN pl.onAccount = 1 THEN -util.ToDecimal172(pl.SalesTaxAmt)
        ELSE util.ToDecimal172(pl.PurchaseAmount)
      END
    ,RefundTransactionId = NULL
FROM
    [cdc].[dbo_PurchaseLog_CT] plc WITH (NOLOCK)    
    INNER JOIN [cdc].[lsn_time_mapping] tm WITH (NOLOCK)  ON plc.__$Start_lsn = tm.Start_lsn
    INNER JOIN dbo.purchaselog pl WITH (NOLOCK) ON plc.Purchaselogid = pl.purchaselogid
    INNER join StoreSet s ON pl.StoreID = s.Storeid
    --NEW CODE HERE ***
    INNER JOIN [cdc].[dbo_PurchaseLog_CT] plc2 WITH (NOLOCK) ON plc2.[__$start_lsn] = plc.[__$start_lsn] AND plc2.PurchaseLogID = plc.PurchaseLogID
WHERE 
    tm.tran_end_time >= @fromDate
    AND tm.tran_end_time < @todate
    --NEW CODE HERE ***
    AND (plc.__$operation = 4 AND plc.DELETED = 1)
    AND (plc2.[__$operation] = 3 AND plc2.Deleted IS NULL)
    --AND plc.__$operation = 4
    AND plc.DELETED IS NULL
    AND pl.RefundID IS NULL

似乎将INNER JOIN添加到自身,加入我指定的那两列,似乎有效。我将要经历几次测试,但似乎这可能已经成功了。感谢任何可能一直在为我看这个的人!