Can I depend on order of output when using row_number()

后端 未结 4 1226
孤独总比滥情好
孤独总比滥情好 2021-01-21 09:09

I believe the answer is no. And am looking for a counter example to show that order of output is not guaranteed, absent an order by clause.

4条回答
  •  余生分开走
    2021-01-21 09:34

    If you want an ordered result set, add an ORDER BY clause to your SELECT. Period. Anything else is circumstantial and may or may not work depending on the current SQL build you're testing, the day's mood of the optimizer and the phase of Mars transit in Pisces.

    A trivial example that contradicts your assumption:

    select orderId, CustomerId, orderDateTime
        , row_number() over (partition by customerId order by orderDateTime) RN
        , row_number() over (partition by orderDateTime order by customerId) AntiRN
    from #order
    

提交回复
热议问题