我在 SQLServer 2008 中遇到了一个我不理解的奇怪行为。我想快速将独特的客户与独特的付款配对。
使用这个查询,我得到了我期望的结果。每个 CustomerId 都与不同的 PaymentId 配对。
SELECT CustomerId, PaymentId, RowNumber1, RowNumber2
FROM (
SELECT
c.Id as CustomerId,
p.Id as PaymentId,
ROW_NUMBER() OVER (PARTITION BY p.Id ORDER BY p.Id) AS RowNumber1,
ROW_NUMBER() OVER (PARTITION BY c.Id ORDER BY c.Id) AS RowNumber2
FROM Customer as c
CROSS JOIN Payment as p
) AS INNERSELECT WHERE RowNumber2 = 1
+------------+-----------+------------+------------+
| CustomerId | PaymentId | RowNumber1 | RowNumber2 |
+------------+-----------+------------+------------+
| 4 | 1 | 1 | 1 |
| 5 | 2 | 2 | 1 |
+------------+-----------+------------+------------+
但是,如果我从外部选择中删除 RowNumber1 列,结果似乎会发生变化。现在 PaymentId 的每个值都是 1,即使我没有触及内部 select 语句。
SELECT CustomerId, PaymentId, RowNumber2
FROM (
SELECT
c.Id as CustomerId,
p.Id as PaymentId,
ROW_NUMBER() OVER (PARTITION BY p.Id ORDER BY p.Id) AS RowNumber1,
ROW_NUMBER() OVER (PARTITION BY c.Id ORDER BY c.Id) AS RowNumber2
FROM Customer as c
CROSS JOIN Payment as p
) AS INNERSELECT WHERE RowNumber2 = 1
+------------+-----------+------------+
| CustomerId | PaymentId | RowNumber2 |
+------------+-----------+------------+
| 4 | 1 | 1 |
| 5 | 1 | 1 |
+------------+-----------+------------+
谁能向我解释为什么从外部选择中删除一列会导致 PaymentId 列中的值发生变化?我可以使用什么其他方法来实现我想要的目标而不需要结果集中的行号?