TSQL ROW_NUMBER() OVER (PARTITION BY... ORDER BY....)

TSQL ROW_NUMBER() OVER (PARTITION BY... ORDER BY....)

我可以 row_number 不分区吗? (请参阅 post 末尾的数据)我可以使用此语句获取 ORDER#。

     ROW_NUMBER() OVER (PARTITION BY LOG_ID ORDER BY ORDER_ID) 

有什么获取 LOG# 的建议吗?

-LOG#   ORDER#  LOG_ID  ORDER_ID
-1      1       340580  387373215
-1      2       340580  387373225
-2      1       340925  387812330
-3      1       340935  388093450
-4      1       340945  387615845
-5      1       340990  386433405
-6      1       341675  376247120
-6      2       341675  376247130
-6      3       341675  388352445
SELECT
    - DENSE_RANK() OVER (ORDER BY LOG_ID) AS "#LOG",
    ROW_NUMBER() OVER (PARTITION BY LOG_ID ORDER BY ORDER_ID) AS "#ORDER",
    LOG_ID,
    ORDER_ID
FROM UnnamedTable
declare @yourtable table ([ORDER#] int, LOG_ID int, ORDER_ID bigint);
insert @yourtable values
(1,340580,387373215),
(2,340580,387373225),
(1,340925,387812330),
(1,340935,388093450),
(1,340945,387615845),
(1,340990,386433405),
(1,341675,376247120),
(2,341675,376247130),
(3,341675,388352445);


select [-LOG#] = -dense_rank() over (order by LOG_ID), *
from @yourtable;

Returns

-LOG#                ORDER#      LOG_ID      ORDER_ID
-------------------- ----------- ----------- --------------------
-1                   1           340580      387373215
-1                   2           340580      387373225
-2                   1           340925      387812330
-3                   1           340935      388093450
-4                   1           340945      387615845
-5                   1           340990      386433405
-6                   1           341675      376247120
-6                   2           341675      376247130
-6                   3           341675      388352445

编辑:@bacon bits 击败了我。 :(