如何通过SQL Server中的值IN()运算符对结果进行排序

时间:2017-08-30 03:08:50

标签: sql sql-server tsql

我想通过SELECT中给出的值来排序IN()语句的结果。

我在IN运算符中有200多个值。

伪代码:

select * 
from EmployeeId 
where EmployeeId in (2, 198, 5,...till 200)

我想要实现的结果集:

EmployeeId
----------    
     2
   198
     5
     .
     .
     .
   200

到目前为止我的研究:当我使用this时,我无法在[]运算符中添加方括号(IN)。

更新:我在excel中粘贴in()运算符中的值。

提前致谢。

2 个答案:

答案 0 :(得分:3)

2件事...... 1)IN操作员不提供任何排序​​功能。 2)由于性能方面的考虑,使用IN运算符使用多个值并不是一个好主意。如果OR条件,IN运算符被评估为是一个序列。所以基本上,它相当于写出200列= 2 OR列= 198 ......依此类推。

鉴于你的目标,我建议使用#temp表。这将提高性能,允许您进行排序,如果您将排序列分配为PK,您将能够对最终结果进行排序,而无需在最终查询的执行计划中进行排序操作(不错小奖金)......

IF OBJECT_ID('tempdb..#SearchValues', 'U') IS NOT NULL 
DROP TABLE #SearchValues;

CREATE TABLE #SearchValues (
    Sort INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED,
    Search INT NOT NULL 
    );
INSERT #SearchValues (Search) VALUES 
    (2),(198),(5),(75),(22),(300),(4),
    (122),(201),(40),(200);

SELECT 
    e.*
FROM
    dbo.Employee e
    JOIN #SearchValues sv
        ON e.EmployeeId = sv.Search
ORDER BY
    sv.Sort;

答案 1 :(得分:0)

这是条件顺序的完成方式。

Order by 
case 
when EmployeeId = 2 then 1
when EmployeeId = 198 then 2
...
End

修改

假设您将此逗号分隔的字符串作为变量,或者即使您不是将它存储为变量,也可以通过使用字符串拆分器来避免您必须手动创建临时表。

declare @table table(col1 int)

insert into @table
values
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)

declare @searchVariable varchar (256) = '10,12,3,5,7,1'

select
    t.* 
from 
    @table t
cross apply
    dbo.DelimitedSplit8K (@searchVariable,',') s
where
    s.Item = t.col1
order by 
    s.ItemNumber    

我使用的分割功能来自Jeff Moden,并在下面添加。与许多其他拆分功能相比,它具有很好的功能。

CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO