构造返回顺序的单个select语句取决于SQL Server中列的值

时间:2017-08-19 10:22:34

标签: sql sql-server sql-order-by

Table1
Id bigint primary key identity(1,1)
Status nvarchar(20)

插入虚拟数据

Insert into Table1 values ('Open') --1
Insert into Table1 values ('Open') --2
Insert into Table1 values ('Grabbed') --3
Insert into Table1 values ('Closed') --4
Insert into Table1 values ('Closed') --5
Insert into Table1 values ('Open') --6

如何构建单个select语句,该语句对数据进行排序,其中'Grabbed'状态的记录是第一个,后跟'Closed',后跟SQL Server中的'Open'

输出:

Id    Status
3     Grabbed
4     Closed
5     Closed
1     Open
2     Open
6     Open

4 个答案:

答案 0 :(得分:1)

我认为你需要这样的东西:

select *
from yourTable
order by case when Status = 'Grabbed' then 1
              when Status = 'Closed' then 2
              when Status = 'Open' then 3
              else 4 end
    , Id;

[SQL Fiddle Demo]

另一种方法是像这样使用CTE:

;with cte as (
  select 'Grabbed' [Status], 1 [order]
  union all select 'Closed', 2
  union all select 'Open', 3
)
select t.*
from yourTable t
left join cte 
  on t.[Status] = cte.[Status]
order by cte.[order], Id;

[SQL Fiddle Demo]

答案 1 :(得分:1)

使用正确的规范化设计可以做得更好:

请勿将Status存储为文字内容。想象一下拼写错误(Grabed行......)

更多的查找表允许您添加边数据,例如排序顺序。

CREATE TABLE StatusLookUp(StatusID INT IDENTITY PRIMARY KEY /*you should name your constraints!*/
                         ,StatusName VARCHAR(100) NOT NULL
                         ,SortRank INT NOT NULL)
INSERT INTO StatusLookUp VALUES
 ('Open',99) --ID=1
,('Closed',50)--ID=2
,('Grabbed',10)--ID=3

CREATE TABLE Table1(Id bigint primary key identity(1,1) /*you should name your constraints!*/
                   ,StatusID INT FOREIGN KEY REFERENCES StatusLookUp(StatusID));

Insert into Table1 values (1) --1
Insert into Table1 values (1) --2
Insert into Table1 values (3) --3
Insert into Table1 values (2) --4
Insert into Table1 values (2) --5
Insert into Table1 values (1) --6

SELECT *
FROM Table1 AS t1
INNER JOIN StatusLookUp AS s ON t1.StatusID=s.StatusID
ORDER BY s.SortRank;

答案 2 :(得分:1)

我发现最简单的方法是使用字符串:

unit module foo;
sub foo-greet($who)  is export(:greet)  { ... }
sub foo-greet2($who) is export(:greet2) { ... }
sub foo-greet3($who) is export(:greet3) { ... }

或:

order by charindex(status, 'Grabbed,Closed,Open')

如果您要在查询中添加值,我认为最简单的方法是使用order by charindex(',' + status + ',', ',Grabbed,Closed,Open,')

values()

最后。这种需求表明你应该有一个状态参考表。不是将字符串名称放在其他表中,而是放入一个id。参考表可以具有优先级以及其他信息。

答案 3 :(得分:0)

试试这个:

select Id,status from tablename where status='Grabbed'
union
select Id,status from tablename where status='Closed'
union
select Id,status from tablename where status='Open'