拆分一串无限长度的SQL

时间:2013-10-01 14:06:05

标签: sql split delimited-text

我有一个数据列,其值如下:

表1

ID|GROUPNAME         |MEMBER  
1|GRP1_ML_Unit1_Role1|GRP=User1,DC=com;GRP=User2,DC=com  
2|GRP2_ML_Unit2_Role2|GRP=User3,DC=com;GRP=User4,DC=com;GRP=User5,DC=com  
3|GRP3_ML_Unit3_Role3|GRP=User6,DC=com;GRP=User7,DC=com;GRP=User8,DC=com;GRP=User8,DC=com

预期输出

ID|GRP1 |GRP2|GRP3 |GRP4 |MEM1            |MEM2            |MEM3            |MEM4|MEM5|  
1 |GRP1 |ML  |Unit1|Role1|GRP=User1,DC=com|GRP=User2,DC=com|                |    |  
2 |GRP2 |ML  |Unit2|Role2|GRP=User3,DC=com|GRP=User4,DC=com|GRP=User5,DC=com|    |  
3 |GRP3 |ML |Unit3|Role3|GRP=User6,DC=com|GRP=User7,DC=com|GRP=User8,DC=com|GRP=User8,DC=com    |  

谢谢, RYL

1 个答案:

答案 0 :(得分:0)

完整的解决方案如下,并附有您提供给我的样本数据。

首先,创建一个临时表并用数据填充它。

-- Drop the table
drop table #member;
go

-- Sample table
create table #member
(
  member_id int not null,
  group_name varchar(256),
  member_data varchar(8000)
);
go

-- Sample data
insert into #member values
(1, 'GRP1_ML_Unit1_Role1', 'GRP=User1,DC=com;GRP=User2,DC=com'),  
(2, 'GRP2_ML_Unit2_Role2', 'GRP=User3,DC=com;GRP=User4,DC=com;GRP=User5,DC=com'),
(3, 'GRP3_ML_Unit3_Role3', 'GRP=User6,DC=com;GRP=User7,DC=com;GRP=User8,DC=com;GRP=User8,DC=com');
go

-- Show the data
select * from #member;
go

其次,复制其中一个字符串分离器。我最终安装了Jeff Moden的字符串拆分器,用于8K最大字符串。

查询几乎就在那里。但是,我们想要的每一列都是一行。我们需要动态地转动表格。

-- 
-- Almost there!
--

-- Data in columns, instead of rows
select m.member_id,  m.group_name, s.Item as cols_data, 'MEM' + cast(s.ItemNumber as varchar(6)) as cols_name from #member as m
CROSS APPLY dbo.DelimitedSplit8k(m.member_data,';') s
go

最后但并非最不重要的是,弄清楚列数。编写动态TSQL来转动我们的数据并得到我们的结果。

--
-- Write dynamic sql to solve
--

DECLARE 
    @cols AS nvarchar(MAX),
    @query AS nvarchar(MAX);

-- Get a dynamic number of columns
SET @cols = STUFF(
  (
     SELECT distinct ',' + QUOTENAME(c.cols_name) 
     FROM 
     (
        select m.member_id,  m.group_name, s.Item as cols_data, 'MEM' + cast(s.ItemNumber as varchar(6)) as cols_name from #member as m
        CROSS APPLY dbo.DelimitedSplit8k(m.member_data,';') s
     ) as c
     FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
   ,1,1,'');
print @cols;

-- Make dynamic pivot query
set @query = 'SELECT member_id as ID1, group_name as GROUP1, ' + @cols + ' from 
            (
                select m.member_id,  m.group_name, s.Item as cols_data, ''MEM'' + cast(s.ItemNumber as varchar(6)) as cols_name from #member as m
                CROSS APPLY dbo.DelimitedSplit8k(m.member_data, '';'') s
           ) x
            pivot 
            (
                max(cols_data)
                for cols_name in (' + @cols + ')
            ) p ';
execute(@query)

以所需格式显示结果的屏幕截图。 enter image description here