web-dev-qa-db-ja.com

T-SQL-(charで区切られた)文字列を行と列に分割する方法

文字で区切られた文字列を行と列に分割する最良の方法は何ですか?

これが私の入力文字列です:

!1;100;10;200;0;500;2;1000;30!2;100;3;500;1;2000;5

そして、これが私の望ましい出力テーブルです:

Id  Value  Count
1   100    10
1   200    0
1   500    2
1   1000   30
2   100    3
2   500    1
2   2000   5

宜しくお願いします、

7
Tpsamw1

Jeff Modenの機能を変更する必要はありません。私は、元の関数を使用するだけで結果を達成するための少し洗練された方法を考案しました。

最初に、これは元のModenの関数です:(ソース: http://www.sqlservercentral.com/articles/Tally+Table/72993/

CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
        (@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
;

そして、必要な結果を取得するためのクエリ(わかりやすくするためにCTEを作成しました)

DECLARE @string VARCHAR(8000)
SET @string = '!1;100;10;200;0;500;2;1000;30!2;100;3;500;1;2000;5'

;WITH CTE AS
(
SELECT 
SUBSTRING(A.Item,1,CHARINDEX(';',A.Item)-1) As Id, 
B.Item
FROM dbo.DelimitedSplit8K(@string, '!') AS A
CROSS APPLY dbo.DelimitedSplit8K(A.Item, ';') AS B
WHERE A.Item <> '' --to avoid blank first row because the first !
)

SELECT Id, [Value], [Count]
FROM
(
SELECT Id, Item As [Value], 
ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Id) As RowNumber,
LEAD(Item,1,0) OVER(PARTITION BY Id ORDER BY Id) As [Count]
FROM CTE
) As T
WHERE RowNumber % 2 = 0

EDITED:私の解決策では、id値が順序付けられているか、元の文字列の順序が結果で保持される必要はなかった。一般的なソリューションの下(状況をテストするために文字列の最初の数値を変更したことに注意してください)

DECLARE @string VARCHAR(8000)
SET @string = '!3;100;10;200;0;500;2;1000;30!2;100;3;500;1;2000;5'
;WITH CTE AS
(
SELECT 
ROW_NUMBER() OVER(ORDER BY (SELECT 0)) As RN,
CAST(SUBSTRING(A.Item,1,CHARINDEX(';',A.Item)-1) AS Varchar(400)) As Id, 
B.Item
FROM dbo.DelimitedSplit8K(@string, '!') AS A
CROSS APPLY dbo.DelimitedSplit8K(A.Item, ';') AS B
WHERE A.Item <> '' --to avoid blank first row because the first !
)

SELECT Id, [Value], [Count]
FROM
(
SELECT Id, Item As [Value], 
ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Id) As RowNumber,
LEAD(Item,1,0) OVER(PARTITION BY Id ORDER BY Id) As [Count]
,FIRST_VALUE(RN) OVER(PARTITION BY Id ORDER BY Id) AS FV
FROM CTE
) As T
WHERE RowNumber % 2 = 0
ORDER BY FV

http://sqlfiddle.com/#!6/31ccd/

2
JGA

Jeff Modenがwww.SQLServerCentral.comに DelimitedSplit8K というスプリッター関数を作成しました。 2つのパラメーター@pString(分割する文字列)と@pDelimiter(区切り文字、あなたの場合は;)を取ります。

ただし、あなたの場合、まっすぐに分割するわけではないので、少し変更する必要があります。実際、2つの異なるタイプの分割を行う必要があります。分割する最初の通常の! 2番目は、最初の値を最初の列として使用し、残りをペアに分割して、行ごとに1ペアとなるように変更して、別の行を取得します。

最初にコードをテストする

DECLARE @splitstring varchar(8000)
SET @splitstring = '!1;100;10;200;0;500;2;1000;30!2;100;3;500;1;2000;5'
-- First use the original function to split out the groups.
-- The STUFF function removes the first ! since this is a split function
-- and if it's left we get a blank line.
SELECT FinalDelim.* 
FROM DelimitedSplit8K(STUFF(@splitstring,1,1,''),'!') AS FirstDelim
-- Here we CROSS APPLY the modified version over each of the original splits
CROSS APPLY ModifiedDelimitedSplit8K(FirstDelim.Item,';') AS FinalDelim
GO

そしてこれがModifiedDelimitedSplit8Kコードです。

-- Based Jeff Modem's original script
CREATE FUNCTION [dbo].[ModifiedDelimitedSplit8K]
--===== Define I/O parameters
        (@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.
-- Putting this in it's own CTE since we need ItemNumber 1 for all of the rows
cteSplitString(ItemNumber, Item) AS (
     SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
            Item       = SUBSTRING(@pString, l.N1, l.L1)
    FROM cteLen l)
SELECT cteItem1.Item AS Id,
    cteItem2.Value,
    cteItem2.Count
FROM (SELECT Item FROM cteSplitString WHERE ItemNumber = 1) AS cteItem1
CROSS JOIN (SELECT MAX(CASE WHEN ItemNumber%2 = 0 THEN Item ELSE NULL END) AS Value,
                    MAX(CASE WHEN ItemNumber%2 = 1 THEN Item ELSE NULL END) AS Count
            FROM cteSplitString 
            WHERE ItemNumber > 1
            GROUP BY ItemNumber/2) AS cteItem2
0
Kenneth Fisher

これはおそらく最良の解決策ではありません。しかし、あなたの提案は少し特別だったので、アドホックソリューションを作成することにしました。楽しかった。ここでテストできます: http://sqlfiddle.com/#!3/9eecb7db59d16c80417c72d1/62

    declare @string varchar(max) 
    set @string = '!1;100;10;200;0;500;2;1000;30!2;100;3;500;1;2000;5'

    declare @t1  table (row varchar(max))

    declare @end int 
    set @end = 1

    set @string = substring ( @string ,2, len(@string))
    while charindex ( '!' ,@string,1) > 0
    begin
        set @end = charindex ( '!' ,@string,1)
        insert into @t1 (row) values (substring ( @string ,1, @end - 1))
        set @string = substring ( @string ,@end+1, len(@string))
    end
    set @end = len(@string)

    insert into @t1 (row) values (substring ( @string ,1, @end))

    declare cur cursor for select * from @t1
    open cur

    declare @row varchar(max)
    fetch next from cur into @row
    declare @result table
    (id varchar(max), [value] varchar(max), [count] varchar(max))
    declare @id varchar(max), @value varchar(max), @count varchar(max)
    while @@fetch_status = 0
    begin
        set @end = charindex( ';' ,@row)
        set @id = substring(@row,1,@end-1)
        set @row = substring(@row,@end +1,len(@row))
        while len(@row) > 0 
        begin
            set @end = charindex( ';' ,@row)
            set @value = substring(@row,1,@end-1)
            set @row = substring(@row,@end +1,len(@row))
            set @end = charindex( ';' ,@row)
            if @end > 0 
            begin
                set @count = substring(@row,1,@end-1)
                set @row = substring(@row,@end +1,len(@row))
                set @end = charindex( ';' ,@row)
            end
            else 
            begin
                set @count = substring(@row,1,len(@row))
                set @row = ''
            end
            insert into @result (id, [value], [count]) values (@id, @value, @count)
        end
        fetch next from cur into @row   
    end
close cur
deallocate cur   
select * from @result
0
JGA