3
我遇到了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 |
+------------+-----------+------------+------------+
但是,如果我從外部select中刪除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 |
+------------+-----------+------------+
任何人都可以向我解釋爲什麼從外部select中刪除列會導致PaymentId列中的值發生更改?我可以使用其他什麼方法來實現我想要的目標,而不需要結果集中的行號?
有什麼辦法給它如何傳中後責令其加盟,使我始終得到像我的第一個例子結果的暗示? – scourge192
檢查更新的答案,希望這有助於 –
謝謝,我甚至沒有想過要這樣做 – scourge192