根据 SQL 列内容将数据插入另一个 table
Base on SQL column content insert the data into another table
我有以下 table 名称:具有以下图像数据的 CustomerContent,并且 My product is:
在 ProductContent 单元格中很常见。第一个冒号不需要拆分 (My product is:
) 如果下一个冒号我们需要拆分 ProductContent 单元格基本文本,请分配如下值。如果传单数据内容CGM
则赋值37.
my table
CustomerId ProductContent
100 My product is: Shoes
101 My product is: Diabetic Shoes
102 My product is: Shoes Back Brace
103 My product is: Dexcom G6 (CGM)
104 My product is: Freestyle Libre (CGM)
105 My product is: Shoes Knee Brace
106 My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes
107 My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)
108 My product is: Freestyle Libre (CGM): Diabetic Shoes
我需要像下面这样的输出并将上面的数据插入另一个 table 名称:CustomerContentTemp 包含列 CusmerId
和 Values
如下格式。
output table
CustomerId Values
100 1
101 1
102 8
103 37
104 37
105 14
106 37
106 37
106 1
107 37
107 37
108 37
108 1
从下面的数据逻辑插入输出 CustomerContentTemp table
Shoes=1
Diabetic Shoes=1
Shoes Back Brace=8
Dexcom G6 (CGM)=37
Freestyle Libre (CGM)=37
Shoes Knee Brace=14
如果 ProductContent 单元格数据不匹配,则插入值 0。
我个人选择的 t-sql 分离器是这个。 https://www.sqlservercentral.com/articles/reaping-the-benefits-of-the-window-functions-in-t-sql-2 还有很多其他选择,很多都有优点和缺点。这对我来说是最健壮的,开销和混乱最少。要自己查找代码,您需要滚动到文章底部(最好阅读整篇文章,以便了解它在做什么)。
现在让我们获取您的数据并使其成为可使用的,这样任何人都可以复制和粘贴它。本文 (How Stuff and 'For Xml Path' work in SQL Server?) 将引导您完成该过程的这一部分。
create table CustomerContent
(
CustomerId int
, ProductContent varchar(500)
)
insert CustomerContent
select 100, 'My product is: Shoes' union all
select 101, 'My product is: Diabetic Shoes' union all
select 102, 'My product is: Shoes Back Brace' union all
select 103, 'My product is: Dexcom G6 (CGM)' union all
select 104, 'My product is: Freestyle Libre (CGM)' union all
select 105, 'My product is: Shoes Knee Brace' union all
select 106, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes' union all
select 107, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)' union all
select 108, 'My product is: Freestyle Libre (CGM): Diabetic Shoes'
create table CustomerContentTemp
(
SimpleName varchar(100)
, Result int
)
insert CustomerContentTemp
select 'Shoes', 1 union all
select 'Diabetic Shoes', 1 union all
select 'Shoes Back Brace', 8 union all
select 'Dexcom G6 (CGM)', 37 union all
select 'Freestyle Libre (CGM)', 37 union all
select 'Shoes Knee Brace', 14
好的。所以现在可以很好地轻松处理数据和拆分器功能。这是您拼图的第一步。您必须将这些数据分开并去掉那个古怪的前缀。
select c.*
, cct.SimpleName
, cct.Result
from CustomerContent c
cross apply dbo.DelimitedSplit8K_LEAD(replace(c.ProductContent, 'My product is: ', ''), ':') x
left join CustomerContentTemp cct on cct.SimpleName = ltrim(x.Item)
根据这个结果,您需要应用使用 STUFF 的逻辑,将其压缩回您需要的非规范化格式。
我有以下 table 名称:具有以下图像数据的 CustomerContent,并且 My product is:
在 ProductContent 单元格中很常见。第一个冒号不需要拆分 (My product is:
) 如果下一个冒号我们需要拆分 ProductContent 单元格基本文本,请分配如下值。如果传单数据内容CGM
则赋值37.
my table
CustomerId ProductContent
100 My product is: Shoes
101 My product is: Diabetic Shoes
102 My product is: Shoes Back Brace
103 My product is: Dexcom G6 (CGM)
104 My product is: Freestyle Libre (CGM)
105 My product is: Shoes Knee Brace
106 My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes
107 My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)
108 My product is: Freestyle Libre (CGM): Diabetic Shoes
我需要像下面这样的输出并将上面的数据插入另一个 table 名称:CustomerContentTemp 包含列 CusmerId
和 Values
如下格式。
output table
CustomerId Values
100 1
101 1
102 8
103 37
104 37
105 14
106 37
106 37
106 1
107 37
107 37
108 37
108 1
从下面的数据逻辑插入输出 CustomerContentTemp table
Shoes=1
Diabetic Shoes=1
Shoes Back Brace=8
Dexcom G6 (CGM)=37
Freestyle Libre (CGM)=37
Shoes Knee Brace=14
如果 ProductContent 单元格数据不匹配,则插入值 0。
我个人选择的 t-sql 分离器是这个。 https://www.sqlservercentral.com/articles/reaping-the-benefits-of-the-window-functions-in-t-sql-2 还有很多其他选择,很多都有优点和缺点。这对我来说是最健壮的,开销和混乱最少。要自己查找代码,您需要滚动到文章底部(最好阅读整篇文章,以便了解它在做什么)。
现在让我们获取您的数据并使其成为可使用的,这样任何人都可以复制和粘贴它。本文 (How Stuff and 'For Xml Path' work in SQL Server?) 将引导您完成该过程的这一部分。
create table CustomerContent
(
CustomerId int
, ProductContent varchar(500)
)
insert CustomerContent
select 100, 'My product is: Shoes' union all
select 101, 'My product is: Diabetic Shoes' union all
select 102, 'My product is: Shoes Back Brace' union all
select 103, 'My product is: Dexcom G6 (CGM)' union all
select 104, 'My product is: Freestyle Libre (CGM)' union all
select 105, 'My product is: Shoes Knee Brace' union all
select 106, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM): Diabetic Shoes' union all
select 107, 'My product is: Dexcom G6 (CGM): Freestyle Libre (CGM)' union all
select 108, 'My product is: Freestyle Libre (CGM): Diabetic Shoes'
create table CustomerContentTemp
(
SimpleName varchar(100)
, Result int
)
insert CustomerContentTemp
select 'Shoes', 1 union all
select 'Diabetic Shoes', 1 union all
select 'Shoes Back Brace', 8 union all
select 'Dexcom G6 (CGM)', 37 union all
select 'Freestyle Libre (CGM)', 37 union all
select 'Shoes Knee Brace', 14
好的。所以现在可以很好地轻松处理数据和拆分器功能。这是您拼图的第一步。您必须将这些数据分开并去掉那个古怪的前缀。
select c.*
, cct.SimpleName
, cct.Result
from CustomerContent c
cross apply dbo.DelimitedSplit8K_LEAD(replace(c.ProductContent, 'My product is: ', ''), ':') x
left join CustomerContentTemp cct on cct.SimpleName = ltrim(x.Item)
根据这个结果,您需要应用使用 STUFF 的逻辑,将其压缩回您需要的非规范化格式。