按节排序定界列

时间:2018-07-24 16:11:10

标签: sql sql-server tsql

我需要按每个定界部分顺序排序一列。例如,给定样本数据:

    a   
----------
120.1
120.2
120.12
120
130
120.2.22
120.2.41
120.3

我需要获得以下输出:

120
120.1
120.2
120.2.22
120.2.41
120.3
120.12
130

我使用此查询,但它不起作用

查询;

Select a from b rpad(REPLACE(a, '.', ''),15,0),REPLACE(a, '.', '') ASC

2 个答案:

答案 0 :(得分:3)

我同意Sean Lange等人的所有评论。由于可以有无限数量的小数,因此需要一个分隔符。拆分值之后,您可以应用与我展示的相同的ORDER BY逻辑。我认为ORDER BY确实可以说明您正在寻找的算法

这是使用splitter的方法:

declare @table table (col varchar(64))
insert into @table
values
('120.1'),
('120.2'),
('120.12'),
('120'),
('130'),
('120.2.22'),
('120.2.41.55.64.12'),
('120.3')

;with cte as(
select
    *
from @table t 
cross apply dbo.DelimitedSplit8K(t.col,'.')
pivot(
max(Item) for ItemNumber in ([1],[2],[3],[4],[5],[6],[7],[8]) --enter the number of possibilities
) p)

select 
    cte.*
from cte
order by
    cast(isnull(cte.[1],0) as int)
    ,cast(isnull(cte.[2],0) as int)
    ,cast(isnull(cte.[3],0) as int)
    ,cast(isnull(cte.[4],0) as int)
    ,cast(isnull(cte.[5],0) as int)
    ,cast(isnull(cte.[6],0) as int)
    ,cast(isnull(cte.[7],0) as int)
    ,cast(isnull(cte.[8],0) as int)

该功能(如果需要)

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

答案 1 :(得分:0)

首先创建一个要拆分的函数。我从https://sqlperformance.com/2012/07/t-sql-queries/split-strings

偷了这个
create FUNCTION dbo.SplitStrings_XML
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT rn=row_number() over (order by i), Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );
GO


declare @t table (a varchar(64))
insert into @t
values
('120.1'),
('120.2'),
('120.12'),
('120'),
('130'),
('120.2.22'),
('120.2.41'),
('120.3')


select a
    , a1 = max(cast(case when rn=1 then Item else null end as int))
    , a2 = max(cast(case when rn=2 then Item else null end as int))
    , a3 = max(cast(case when rn=3 then Item else null end as int))
    , a4 = max(cast(case when rn=4 then Item else null end as int))
from @t t
    cross apply dbo.SplitStrings_XML(t.a,'.') as a
group by t.a
order by 2,3,4,5