按另一个相关行的值进行分区

时间:2015-07-03 16:34:46

标签: sql-server tsql partitioning rank

我试图在每个帐户的表格中识别最新的合并交易。此表填充了帐户合并,最新的合并事务是较大的merge_tx_id

来源表:

| merge_tx_id | merge_from_id | merge_to_id |
|-------------|---------------|-------------|
|           1 |           546 |         100 |
|           2 |           789 |         802 |
|           3 |           123 |         456 |
|          33 |           456 |         123 |
|         222 |           802 |         789 |
|         333 |           123 |         456 |

merge_tx_id IN(3,33,333)是基于另一行中出现的merge_from_id或merge_to_id的相同帐户,在此示例中执行合并,然后再次“未合并”。最新的id应为'456'

| merge_tx_id | merge_from_id | merge_to_id |
|-------------|---------------|-------------|
|           3 |           123 |         456 |
|          33 |           456 |         123 |
|         333 |           123 |         456 |

merge_tx_id IN(2,222)是基于merge_from_id或merge_to_id出现在另一行的同一帐户,最新的是'789'

| merge_tx_id | merge_from_id | merge_to_id |
|-------------|---------------|-------------|
|           2 |           789 |         802 |
|         222 |           802 |         789 |

merge_tx_id IN(1)与任何其他行无关,最新的id为'100'

| merge_tx_id | merge_from_id | merge_to_id |
|-------------|---------------|-------------|
|           1 |           546 |         100 |

我想我需要将表连接到自身并执行RANK但是我不清楚如果可以的话,如何执行它(第1列或第2列)。

期望的结果是

| merge_tx_id | merge_from_id | merge_to_id |
|-------------|---------------|-------------|
|           1 |           546 |         100 |
|         333 |           123 |         456 |
|         222 |           802 |         789 |

示例:http://sqlfiddle.com/#!3/9a94b6/11

1 个答案:

答案 0 :(得分:1)

您可以使用以下查询:

SELECT merge_tx_id, merge_from_id, merge_to_id
FROM (
SELECT merge_tx_id, merge_from_id, merge_to_id,
       ROW_NUMBER() OVER (PARTITION BY x.merge1, y.merge2
                          ORDER BY merge_tx_id DESC) AS rn
FROM merge_grp
CROSS APPLY (SELECT CASE WHEN merge_from_id < merge_to_id 
                            THEN merge_from_id
                            ELSE merge_to_id
                    END AS merge1) AS x
CROSS APPLY (SELECT CASE WHEN merge_from_id >= merge_to_id 
                            THEN merge_from_id
                            ELSE merge_to_id
                         END AS merge2) AS y  ) t
WHERE t.rn = 1                     

该查询使用了两个CROSS APPLY子句,以便将merge_from_idmerge_to_id重新排列为两个等效列,merge1merge2,这些是按升序排列。 ROW_NUMBER使用这两个计算列对行进行分区,从而可以选择每merge_tx_idmerge1最多merge2的行。

Demo here