根据提供的值序列添加行号

时间:2013-03-02 01:16:13

标签: sql sql-server-2008 row-number

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

5 个答案:

答案 0 :(得分: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 Fiddle Demo

Updated Demo包含子匹配。

答案 1 :(得分:2)

这是任何长度代码列表的解决方案。 使用自增量字段和代码创建表。按给定顺序插入。加入表格和订单......

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

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   |
+----+----------+

接下来将Sample表与上面的结果联系起来。 GL

答案 2 :(得分: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上的演示

答案 3 :(得分: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+',%' 

答案 4 :(得分:0)

  

上面已经做了很多改变以包括rownumbers。

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 Fiddle Demo