3

我正在使用 SQL Server 2008 R2。我有以下数据:

ID   Value   OrderNumber
1      A         NULL
2      E          4
3      C         NULL
4      B         NULL
5      F          2
6      D         NULL

我想编写一个查询,该查询必须考虑 OrderNumber 值按 OrderNumber 列获取数据排序。查询结果必须如下:

ID      Value     OrderNumber
1         A           NULL
5         F            2  --indicates row must be second in result set. 
3         C           NULL
2         E            4  --indicates row must be fourth in result set.
4         B           NULL
6         D           NULL

感谢您的阅读和您的回答。

4

3 回答 3

3

我尝试了许多不同的方法,但我能找到的唯一能保证产生所需结果的方法是:

declare @t table (ID int not null,Value char(1) not null,OrderNumber int null)
insert into @T(ID,Value,OrderNumber) values
(1,'A',NULL),
(2,'E',4),
(3,'C',NULL),
(4,'B',NULL),
(5,'F',2),
(6,'D',NULL)

;With Nbrs as (
    select ROW_NUMBER() OVER (ORDER BY ID) as n from @t
), AvailableNbrs as (
    select n,ROW_NUMBER() OVER (ORDER BY n) as rn from Nbrs where n not in (select OrderNumber from @t where OrderNumber is not null)
), RequiredOrders as (
    select ID,ROW_NUMBER() OVER (ORDER BY ID) as rn from @t where OrderNumber is null
)
select
    *,COALESCE(OrderNumber,an.n) as FinalOrder
from
    @t t
        left join
    RequiredOrders ro
        on
            t.ID = ro.ID
        left join
    AvailableNbrs an
        on
            ro.rn = an.rn
order by COALESCE(OrderNumber,an.n)

我们使用一些 CTE 来查找OrderNumber当前未分配的 s,并将那些 1-1 与没有 的行匹配OrderNumber

结果:

|--------- @t --------------| |----- RequiredOrders ---------| |----- AvailableNbrs -------------------| |- COALESCE -------|
ID          Value OrderNumber ID          rn                   n                    rn                   FinalOrder
----------- ----- ----------- ----------- -------------------- -------------------- -------------------- --------------------
1           A     NULL        1           1                    1                    1                    1
5           F     2           NULL        NULL                 NULL                 NULL                 2
3           C     NULL        3           2                    3                    2                    3
2           E     4           NULL        NULL                 NULL                 NULL                 4
4           B     NULL        4           3                    5                    3                    5
6           D     NULL        6           4                    6                    4                    6
于 2012-10-29T14:40:22.813 回答
1

找到了比接受的答案更好的解决方案:

declare @t table(id int, value char, ordernumber int)

insert @t values(1,'A', null)
insert @t values(2,'E',4)
insert @t values(3,'C',NULL)
insert @t values(4,'B',NULL)
insert @t values(5,'F',2)
insert @t values(6,'D',NULL)

;with a as
(
select *, row_number() over (order by id)+.1 rn1 from @t
where ordernumber is null
union all
select *, ordernumber - rank() over (order by ordernumber)+1 rn1 from @t
where ordernumber is not null
)
select * from a order by rn1, ordernumber
于 2012-10-29T15:03:47.610 回答
0

试试这个:

SELECT   *
FROM     tableName
Order BY  CASE WHEN OrderNumber IS NULL THEN ID ELSE OrderNumber END  
于 2012-10-29T14:19:11.920 回答