这是我的T-SQL查询:
SELECT
debt_trans_1.debt_code, debt_trans_1.tx_amount AS new_amount,
debt_trans.tx_amount, debt_trans.packetid,
debt_trans.debt_code AS master_code, debt_trans_2.tx_amount AS rev,
debt_trans.tx_date
FROM
debt_trans
LEFT OUTER JOIN
instplan ON debt_trans.packetid = instplan.packetid
AND debt_trans.tran_code = instplan.tran_code
AND debt_trans.debt_code = instplan.debt_code
FULL OUTER JOIN
debt_trans AS debt_trans_2
LEFT OUTER JOIN
debt_trans AS debt_trans_1 ON debt_trans_2.tx_seq = debt_trans_1.tx_seq + 1
AND debt_trans_2.tx_time = debt_trans_1.tx_time
AND debt_trans_2.tx_date = debt_trans_1.tx_date
AND debt_trans_2.debt_code = debt_trans_1.debt_code
ON debt_trans.packetid = debt_trans_1.packetid
AND debt_trans.tran_code = debt_trans_1.tran_code
AND debt_trans.tx_date = debt_trans_1.tx_date
AND debt_trans.tx_time = debt_trans_1.tx_time
WHERE
(debt_trans.debt_code IN ('MASKEDCODE02', 'MASKEDCODE01'))
AND (debt_trans.tx_date BETWEEN '2019-02-01' AND '2019-02-19')
AND (debt_trans.tran_code = 'DR3015')
ORDER BY
debt_trans_1.debt_code
输出
debt_code new_amount tx_amount packetid master_code rev tx_date
----------------------------------------------------------------------------------------
MASKEDCODEA2 60.00 60.00 3903375 MASKEDCODE02 8.10 2019-02-04 00:00:00.000
MASKEDCODEA2 60.00 60.00 3903375 MASKEDCODE02 8.10 2019-02-04 00:00:00.000
MASKEDCODEA2 10.39 10.39 3903375 MASKEDCODE02 1.40 2019-02-04 00:00:00.000
MASKEDCODEA2 10.39 10.39 3903375 MASKEDCODE02 1.40 2019-02-04 00:00:00.000
MASKEDCODEA1 255.37 379.67 3736780 MASKEDCODE01 39.58 2019-02-01 00:00:00.000
MASKEDCODEB1 310.60 379.67 3736780 MASKEDCODE01 48.14 2019-02-01 00:00:00.000
MASKEDCODEC1 379.67 379.67 3736780 MASKEDCODE01 58.85 2019-02-01 00:00:00.000
您将在这里看到我有重复的条目。我知道我可以在其中使用DISTINCT
选项。但是它们是减少这种情况的另一种方法,因为在此之后,我需要为此添加更多的表。如果现在执行此操作,则会加载更多条目。
理想情况下,它只能输出5条记录。
debt_code new_amount tx_amount packetid master_code rev tx_date
-------------------------------------------------------------------------------
MASKEDCODEA2 10.39 10.39 3903375 MASKEDCODE02 1.40 2019-02-04 00:00:00.000
MASKEDCODEA2 60.00 60.00 3903375 MASKEDCODE02 8.10 2019-02-04 00:00:00.000
MASKEDCODEA1 255.37 379.67 3736780 MASKEDCODE01 39.58 2019-02-01 00:00:00.000
MASKEDCODEB1 310.60 379.67 3736780 MASKEDCODE01 48.14 2019-02-01 00:00:00.000
MASKEDCODEC1 379.67 379.67 3736780 MASKEDCODE01 58.85 2019-02-01 00:00:00.000
提前谢谢
编辑:我猜您的查询是什么样子。
SELECT debt_trans_1.debt_code
, debt_trans_1.tx_amount AS new_amount
, debt_trans.tx_amount
, debt_trans.packetid
, debt_trans.debt_code AS master_code
, debt_trans_2.tx_amount AS rev
, debt_trans.tx_date
FROM debt_trans
LEFT OUTER JOIN instplan ON debt_trans.packetid = instplan.packetid
AND debt_trans.tran_code = instplan.tran_code
AND debt_trans.debt_code = instplan.debt_code
LEFT OUTER JOIN debt_trans AS debt_trans_1 ON debt_trans.packetid = debt_trans_1.packetid
AND debt_trans.tran_code = debt_trans_1.tran_code
AND debt_trans.tx_date = debt_trans_1.tx_date
AND debt_trans.tx_time = debt_trans_1.tx_time
FULL OUTER JOIN debt_trans AS debt_trans_2 ON debt_trans_2.tx_seq = debt_trans_1.tx_seq + 1
AND debt_trans_2.tx_time = debt_trans_1.tx_time
AND debt_trans_2.tx_date = debt_trans_1.tx_date
AND debt_trans_2.debt_code = debt_trans_1.debt_code
WHERE debt_trans.debt_code IN ('MASKEDCODE02', 'MASKEDCODE01')
AND debt_trans.tx_date BETWEEN '2019-02-01' AND '2019-02-19'
AND debt_trans.tran_code = 'DR3015'
ORDER BY debt_trans_1.debt_code