SQL-将字段数据分解为单独的行

SQL-将字段数据分解为单独的行,sql,sql-server,Sql,Sql Server,例如,表alpha中给出了以下内容: 我如何将这些数据分解为: Field1 | Field2 | Field3 ------------------------ Foo | Bar | A Foo | Bar | B Foo | Bar | C Foo | Bar | D 我肯定有一种奇特的加入技巧可以做到这一点,但我想不出来。速度优化不是优先事项-此查询仅用于一次性报告,所以我不介意它是否慢,因为糖蜜让我有机会煮咖啡 您可以尝试以下操作: W

例如,表alpha中给出了以下内容:

我如何将这些数据分解为:

Field1 | Field2 | Field3
------------------------
Foo    | Bar    | A
Foo    | Bar    | B
Foo    | Bar    | C
Foo    | Bar    | D
我肯定有一种奇特的加入技巧可以做到这一点,但我想不出来。速度优化不是优先事项-此查询仅用于一次性报告,所以我不介意它是否慢,因为糖蜜让我有机会煮咖啡

您可以尝试以下操作:

WITH CTE_LenF3 AS 
(
  -- find the length of each field3
  SELECT Field1, Field2, LEN(Field3) as Len_F3
  FROM alpha
)
,CTE_Numbers AS 
(
 --generate numbers from 1 to LEN(Filed3) for each field1,field2 combination
  SELECT Field1, Field2, 1 AS Nmb FROM CTE_LenF3
  UNION ALL
  SELECT c.Field1, c.Field2, Nmb + 1 FROM CTE_Numbers n
  INNER JOIN CTE_LenF3 c ON c.Field1 = n.Field1 AND c.Field2 = n.Field2
  WHERE Nmb + 1 <= LEN_F3
)
--join generated numbers with actual table and use substring to get the characters
SELECT a.Field1, a.Field2, SUBSTRING(a.Field3, n.Nmb, 1)
FROM CTE_Numbers n
INNER JOIN alpha a ON a.Field1 = n.Field1 AND a.Field2 = n.Field2
ORDER BY a.Field1, a.Field2, n.Nmb
您可以尝试以下操作:

WITH CTE_LenF3 AS 
(
  -- find the length of each field3
  SELECT Field1, Field2, LEN(Field3) as Len_F3
  FROM alpha
)
,CTE_Numbers AS 
(
 --generate numbers from 1 to LEN(Filed3) for each field1,field2 combination
  SELECT Field1, Field2, 1 AS Nmb FROM CTE_LenF3
  UNION ALL
  SELECT c.Field1, c.Field2, Nmb + 1 FROM CTE_Numbers n
  INNER JOIN CTE_LenF3 c ON c.Field1 = n.Field1 AND c.Field2 = n.Field2
  WHERE Nmb + 1 <= LEN_F3
)
--join generated numbers with actual table and use substring to get the characters
SELECT a.Field1, a.Field2, SUBSTRING(a.Field3, n.Nmb, 1)
FROM CTE_Numbers n
INNER JOIN alpha a ON a.Field1 = n.Field1 AND a.Field2 = n.Field2
ORDER BY a.Field1, a.Field2, n.Nmb

您可以通过以下步骤轻松完成此操作:

步骤1:创建一个sql表值函数,可以将单词拆分为字符。您可以通过运行以下脚本来完成

CREATE FUNCTION [dbo].[SPLITWORD](
@WORD VARCHAR(MAX) 
) RETURNS @words TABLE (item VARCHAR(8000))

 BEGIN
 declare @count int, @total int
 select @total = len(@WORD), @count = 0

 while @count <= @total
 begin
   insert into @words select substring(@WORD, @count, 1)
   select @count = @count + 1
 end

 RETURN
END

您可以通过以下步骤轻松完成此操作:

步骤1:创建一个sql表值函数,可以将单词拆分为字符。您可以通过运行以下脚本来完成

CREATE FUNCTION [dbo].[SPLITWORD](
@WORD VARCHAR(MAX) 
) RETURNS @words TABLE (item VARCHAR(8000))

 BEGIN
 declare @count int, @total int
 select @total = len(@WORD), @count = 0

 while @count <= @total
 begin
   insert into @words select substring(@WORD, @count, 1)
   select @count = @count + 1
 end

 RETURN
END
比如:

declare @alpha table (Field1 varchar(20), Field2 varchar(20), Field3 varchar(6))
insert into @alpha(Field1, Field2, Field3) values
('Foo','Bar','ABCD')

;With Numbers(n) as (
    select 1 union all select 2 union all select 3 union all
    select 4 union all select 5 union all select 6
)
select Field1,Field2,SUBSTRING(Field3,n,1)
from
    @alpha
        inner join
    Numbers
        on
            n <= LEN(Field3)
比如:

declare @alpha table (Field1 varchar(20), Field2 varchar(20), Field3 varchar(6))
insert into @alpha(Field1, Field2, Field3) values
('Foo','Bar','ABCD')

;With Numbers(n) as (
    select 1 union all select 2 union all select 3 union all
    select 4 union all select 5 union all select 6
)
select Field1,Field2,SUBSTRING(Field3,n,1)
from
    @alpha
        inner join
    Numbers
        on
            n <= LEN(Field3)
替换分隔符


用以下查询实现功能的任何方式替换分隔符

DECLARE @temp as table(newFiled3 varchar(1))
DECLARE @str_Value varchar(50),@count int,@i int=1
SET @str_Value=(SELECT Field3 FROM alpha)
SET @count=LEN(@str_Value)
WHILE(@i<=@count)
BEGIN
    INSERT INTO  @temp VALUES (SUBSTRING ( @str_Value ,@i , 1 ))
SET @i=@i+1
END


SELECT Field1,Field2,b.newFiled3 
FROM tblStudent a inner join @temp b ON a.Field1='Foo'

任何方式的循环都不是一个好方法,但我们仍然需要使用它,因为您的文件3 ABCD是动态的

以下查询以任何方式实现您的功能

DECLARE @temp as table(newFiled3 varchar(1))
DECLARE @str_Value varchar(50),@count int,@i int=1
SET @str_Value=(SELECT Field3 FROM alpha)
SET @count=LEN(@str_Value)
WHILE(@i<=@count)
BEGIN
    INSERT INTO  @temp VALUES (SUBSTRING ( @str_Value ,@i , 1 ))
SET @i=@i+1
END


SELECT Field1,Field2,b.newFiled3 
FROM tblStudent a inner join @temp b ON a.Field1='Foo'

无论如何,循环不是一个好方法,但我们仍然需要使用它,因为您的文件3 ABCD是动态的

字段3是否由任何东西(例如逗号)删除,或者仅仅是每个字符或…?字段3中的字符数是否固定?是否需要按每个字符拆分列3,或者是否有其他内容?可能的重复项没有delimeters,不固定,但最多6个字符,由每个字符分割字段3由任何字符(例如逗号)分割,或仅为每个字符或…?字段3中的字符数是否固定?是否需要按每个字符分割列3,或是否有其他内容?没有delimeters的可能重复,不固定,但最多6个字符,按每个字符分割