必须根据列值在结果集中显示该行的相同顺序

时间:2012-10-29 14:01:24

标签: sql sql-server-2008-r2

我正在使用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

感谢阅读和你的回答。

3 个答案:

答案 0 :(得分: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,并将这些CT与没有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

答案 1 :(得分: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

答案 2 :(得分:0)

试试这个:

SELECT   *
FROM     tableName
Order BY  CASE WHEN OrderNumber IS NULL THEN ID ELSE OrderNumber END