Sql 按节对分隔列排序

Sql 按节对分隔列排序,sql,sql-server,tsql,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, '.', '') A

我需要按每个分隔的部分顺序排列一列。例如,给定示例数据:

    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

我同意等人的所有评论。因为你可以有无限数量的小数,你需要一个拆分器。一旦这些值被拆分,您就可以按照我所展示的逻辑应用相同的顺序。我认为ORDER BY确实解释了您要寻找的算法

下面是一个解决问题的方法:

如果需要,该功能:


首先创建一个要拆分的函数。我从你那儿偷了这个


为什么120.3在120.12之前?原因是您将多个值塞入一个列中,然后期望结果的顺序正确。最好的解决方案是修复数据结构。一切都是数字吗?意思是你根本没有任何字符数据?@scsimon,因为3<12。他们想对字符串排序,但使用数字排序规则。@SeanLange先生,我不知道点值。我不能一列一列地插入它。因为用户可以插入像22.33这样的值,用户可以插入2.13.44.22.1.7.8.2.34.2点。点不受限制。您最好使用客户端。我会说你可以利用很多;但是不同数量的分段将使其变得非常笨拙。没关系,只是注意到MySQL标签被删除了;MS SQL中可能有更好的选项。您必须强制转换为int而不是varchar。这已经很接近了,但仍然不够。例如,120.2.22排序高于120.1您似乎面临着与我相同的挑战。这并没有得到正确的顺序。看看130和120.2.22。但是…颠倒顺序就行了。按1,2,3,4,5,6,7,8的顺序修复@SeanAngelie和你一样@SeanLange我之所以感兴趣,是因为我以前见过这个,但记不起一个好的解决方案。是的,你的解决方案很好。我差一点就到了,但正是这个支点把我带到了终点线。我已将您的脚本添加到我的代码段库中。我当然会给你信用时,如果我张贴一个解决方案再次使用。做得好。
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
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