【问题标题】:Base on SQL column content insert the data into another table基于 SQL 列内容将数据插入到另一个表中
【发布时间】:2022-01-01 09:30:10
【问题描述】:

我有下面的表名:带有以下图像数据的 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

我需要如下输出并将上述数据插入另一个表名:CustomerContentTemp 包含列CusmerIdValues,如下格式。

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 表的数据逻辑

Shoes=1
Diabetic Shoes=1
Shoes Back Brace=8
Dexcom G6 (CGM)=37
Freestyle Libre (CGM)=37
Shoes Knee Brace=14

如果 ProductContent 单元格数据不匹配,则插入值 0。

【问题讨论】:

  • 根据问题指南,请不要发布代码、数据、错误消息等的图像 - 将文本复制或键入问题中。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。
  • 除了是难以处理的图像之外,您在这里的输入和输出完全没有意义。但是,通过在单个元组中存储多个值,您似乎也违反了原始数据中的 1NF。更糟糕的是,您似乎想在输出中继续这样做。
  • 我有更新问题。 @DaleK
  • 我有更新问题。 @SeanLange
  • 只需使用替换从您的数据中删除该前缀。老实说,我越看这个越可笑。您在每个表中都有非规范化数据。如果您正确设计了数据库,那么您面临的整个问题都不会成为问题。

标签: sql sql-server entity-framework


【解决方案1】:

我个人对 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 的逻辑,以将其压缩回您需要的非规范化格式。

【讨论】:

  • 感谢您的完美回答。 @肖恩兰格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2023-03-04
  • 2020-05-22
  • 1970-01-01
相关资源
最近更新 更多