3
SELECT Code, Value FROM dbo.Sample

输出:

 Code     Value
 Alpha    Pig
 Beta     Horse
 Charlie  Dog
 Delta    Cat
 Echo     Fish

我想通过指定代码列表来添加序列列,并根据 IN 子句中指定的顺序对列表进行排序。

SELECT Code, Value FROM dbo.Sample
WHERE Code in ('Beta', 'Echo', 'Alpha')

如果这样更容易,我可以在顶部声明一个变量来指定代码。

关键是我想根据我指定的顺序添加行号。

输出:

 Row   Code    Value
  1    Beta    Horse
  2    Echo    Fish
  3    Alpha   Pig

编辑:之后我意识到我的代码都是固定长度的,这对它的完成方式有很大的影响。我将下面的答案标记为正确,但我的解决方案是使用逗号分隔的值字符串:

DECLARE @CodeList TABLE (Seq int, Code nchar(3))
DECLARE @CodeSequence varchar(255)
DECLARE @ThisCode char(3)
DECLARE @Codes int
SET @Codes = 0

-- string of comma-separated codes
SET @CodeSequence = 'ZZZ,ABC,FGH,YYY,BBB,CCC'

----loop through and create index and populate @CodeList
WHILE @Codes*4 < LEN(@CodeSequence)
BEGIN
    SET @ThisCode = SUBSTRING(@CodeSequence,@Codes*4+1,3)
    SET @Codes = @Codes + 1
    INSERT @CodeList (Seq, Code) VALUES (@Codes, @ThisCode)
END

SELECT Seq, Code from @CodeList
4

5 回答 5

4

这是我见过的唯一两种准确工作的方法:

第一次使用CHARINDEX(类似于 Gordon 的,但我认为 WHERE 语句使用更准确IN):

SELECT *
FROM Sample
WHERE Code IN ('Beta','Echo','Alpha')
ORDER BY CHARINDEX(Code+',','Beta,Echo,Alpha,')

将逗号与代码连接应确保子匹配不会影响结果。

或者,您可以使用以下CASE语句:

SELECT *
FROM Sample
WHERE Code in ('Beta','Echo','Alpha')
ORDER BY CASE 
  WHEN Code = 'Beta' THEN 1
  WHEN Code = 'Echo' THEN 2
  WHEN Code = 'Alpha' THEN 3
END

SQL 小提琴演示

更新了带有子匹配的演示。

于 2013-03-02T01:45:50.920 回答
2

这是任何长度代码列表的解决方案。创建具有自增字段和代码的表。按给定顺序插入。加入表格并按...排序

一些细节。请阅读。你会发现那里有从字符串(用逗号分隔)创建具有自动增量字段的表的函数,即

mysql> call insertEngineer('dinusha,nuwan,nirosh');
Query OK, 1 row affected (0.12 sec)

mysql> select * from engineer;
+----+----------+
| ID | NAME     |
+----+----------+
|  1 | dinusha  |
|  2 | nuwan    |
|  3 | nirosh   |
+----+----------+

接下来将您的示例表与上述结果连接起来。总帐

于 2013-03-02T01:35:28.030 回答
2

您也可以将值用作表源

SELECT Row, Code, Value
FROM [Sample] s JOIN (       
                      SELECT ROW_NUMBER() OVER(ORDER BY(SELECT 1)) AS Row, Match
                      FROM (VALUES ('Beta'), 
                                   ('Echo'), 
                                   ('Alpha'))
                      x (Match)                             
                      ) o ON s.Code = o.Match
ORDER BY Row   

SQLFiddle上的演示

于 2013-03-02T12:06:55.920 回答
0

我可能很想使用字符串函数来做到这一点:

declare @list varchar(8000) = 'Beta,Echo,Alpha';

with Sample as (
    select 'Alpha' as Code, 'Pig' as Value union all
    select 'Beta', 'Horse' union all
    select 'Charlie', 'Dog' union all
    select 'Delta', 'Cat' union all
    select 'Echo', 'Fish'
)
select * from Sample
where charindex(Code, @list) > 0
order by charindex(Code, @list)

如果您担心子匹配,只需执行“分隔符”技巧:

where @list like '%,'+Code+',%' 
于 2013-03-02T01:22:27.543 回答
0

只需对上面所做的进行一点更改以包括行号。

SELECT  CASE   
          WHEN Code = 'BetaBeta' THEN 1  
          WHEN Code = 'Beta' THEN 2  
          WHEN Code = 'Alpha' THEN 3  
        END CodeOrder,  
        *  
FROM Sample  
WHERE Code in ('BetaBeta','Beta','Alpha')  
ORDER BY CodeOrder  

SQL 小提琴演示

于 2013-03-02T05:43:06.730 回答