我有下表名稱:帶有以下影像資料的 CustomerContent,My product is:在 ProductContent 單元格中很常見。My product is:如果下一個冒號我們需要拆分 ProductContent Cell 基礎文本,則不需要拆分第一個冒號( ) 分配如下值。如果滑動資料內容CGM則分配值 37。
我的桌子
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 包含列CusmerId和Values下面的格式。
輸出表
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。
uj5u.com熱心網友回復:
我個人對 t-sql 拆分器的選擇是這個。https://www.sqlservercentral.com/articles/reaping-the-benefits-of-the-window-functions-in-t-sql-2還有很多其他選擇,而且很多都有優點和缺點。這對我來說是最健壯的,開銷和混亂最少。要自己找到代碼,您需要滾動到文章底部(最好閱讀整篇文章以便您了解它在做什么)。
現在讓我們獲取您的資料并使其成為可消費的,這樣任何人都可以復制和粘貼它。這篇文章(SQL Server 中的 Stuff 和“For Xml Path”如何作業?)將引導您完成該程序的這一部分。
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 的邏輯來將其壓縮回您需要的非規范化格式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/363533.html
標籤:sql sql-server 实体框架
上一篇:如何根據處理順序更新日期列?
