嵌套选择返回奇怪结果中的分区

时间:2013-08-21 19:11:28

标签: sql sql-server tsql

我在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 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列中的值发生变化吗?如果不需要结果集中的行号,我还可以用什么方法来实现我想要的目标?

1 个答案:

答案 0 :(得分:4)

这是因为子查询中的row_number()顺序通常没有定义。 进行交叉连接时,行可以按任何顺序

可能是:

CUSTOMERID PAYMENTID
         4         1    
         4         2    
         5         2    
         5         1

或者可能是

CUSTOMERID PAYMENTID
         4         1    
         4         2    
         5         1    
         5         2

当你通过CUSTOMERID计算第一个记录集分区中的row_number时,你得到

CUSTOMERID PAYMENTID ROWNUMBER
         4         1         1  
         4         2         2
         5         2         1
         5         1         2

当你通过CUSTOMERID计算第二个记录集分区中的row_number时,你得到

CUSTOMERID PAYMENTID ROWNUMBER
         4         1         1  
         4         2         2
         5         1         1
         5         2         2

如果您只是将随机客户和付款配对,则可以执行此操作

with cte_cust as (
    select id, row_number() over (order by id) as row_num
    from Customer
), cte_pay as (
    select id, row_number() over (order by id) as row_num
    from Payment
)
select
    c.id as CustomerId,
    p.id as PaymentId
from cte_cust as c
    inner join cte_pay as p on p.row_num = c.row_num

请注意,如果您的客户数多于付款数,则部分客户不会显示在结果中(反之亦然)。

sql fiddle demo