我有两个表,其中第一个表是Englishgermankwds_tbl
,第二个表是Kwd_UploadRecored
表的数据如下
表Englishgermankwds_tbl
English_Keywords German_Keywords
architecture Architektur
Arrival Ankunft
aspirations Lebensziel
attire Kleidung
Blueprint Technische Zeichnung
Carrying Tragen
caucasian appearance Europäischer Abstammung
cheerful fröhlich
clothes Kleidung
color image Farbbild
day Tag
Development Entwicklung
differential focus Geringe Tiefenschärfe
focus on foreground Fokus auf den Vordergrund
front view Vorderansicht
Full Length Ganzkörperansicht
growth Wachstum
Happiness Glücklichsein,Glück
Hardhat Bauarbeiterhelm
表Kwd_UploadRecored
ID Primary_Kwd Sec_Kwd Main_Kwd
1 Man,One Man,architecture,Boy Arrival,Sigle Man , Business Man ,Male aspirations,One Person
2 Woman,attire,Girl Girl,Girls,Female,Blueprint,Carrying, Teenage Girl,Only Girls
3 Grand father,Man,caucasian appearance cheerful, Family,Fatherhood,Family Member, Male Parent,
4 Baby ,clothes,color image growth,Babies,Child,Happiness Children,Toddlers,differential focus,
我想匹配表Kwd_UploadRecored
中表Englishgermankwds_tbl
的所有英语关键字,并将查找关键字替换为德语关键字。
预期结果如 -
ID Primary_Kwd Sec_Kwd Main_Kwd
1 Man,One Man,Architektur,Boy Ankunft,Sigle Man , Business Man ,Male Lebensziel,One Person
2 Woman,Kleidung,Girl Girl,Girls,Female,Technische Zeichnung, Teenage Girl,Only Girls
3 Grand father,Man,Europäischer Abstammung fröhlich, Family,Fatherhood,Family Member, Male Parent,
4 Baby ,clothes,Farbbild Wachstum,Babies,Child,Glücklichsein Children,Toddlers,differential focus,
请帮助如何实现这一目标。
答案 0 :(得分:4)
我在查询中编写了逻辑
;WITH CTE AS
(
SELECT LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT CAST ('<M>' + REPLACE(Primary_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
UNION ALL
SELECT LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT CAST ('<M>' + REPLACE(Sec_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
UNION ALL
SELECT LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT CAST ('<M>' + REPLACE(Main_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)
SELECT T.English_Keywords, T.German_Keywords
FROM CTE C
JOIN Englishgermankwds_tbl T ON C.KeyWords=T.English_Keywords
<强>更新强>
以下是执行预期输出的查询。
;WITH CTE AS
(
-- Since CSV values is scattered with non-alphabetical order, we use ROW_NUMBER()
-- to maintain the order by default
SELECT *,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY (SELECT(0))) RNO,'Primary_Kwd' Colum
FROM
(
-- Convert CSV to rows
SELECT ID,LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT ID,CAST ('<M>' + REPLACE(Primary_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM #Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)TAB
UNION ALL
SELECT *,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY (SELECT(0))) RNO,'Sec_Kwd'
FROM
(
SELECT ID,LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT ID,CAST ('<M>' + REPLACE(Sec_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM #Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)TAB
UNION ALL
SELECT *,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY (SELECT(0))) RNO,'Main_Kwd'
FROM
(
SELECT ID,LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'KeyWords'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT ID,CAST ('<M>' + REPLACE(Main_Kwd, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM #Kwd_UploadRecored
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)TAB
)
,CTE2 AS
(
-- Check for German word, if matched German word else English
SELECT C.ID,C.RNO,C.Colum,ISNULL(T.German_Keywords,C.KeyWords) German_Keywords
FROM CTE C
LEFT JOIN #Englishgermankwds_tbl T ON C.KeyWords=T.English_Keywords
)
,CTE3 AS
(
-- Convert back to CSV values with the old order of strings
SELECT ID,COLUM,
SUBSTRING(
(SELECT ', ' + German_Keywords
FROM CTE2
WHERE C2.Id=Id AND C2.COLUM=COLUM
ORDER BY RNO
FOR XML PATH('')),2,200000) German_Keywords
FROM CTE2 C2
)
-- Now we convert back Primary_Kwd,Sec_Kwd,Main_Kwd to columns with CSV values
SELECT ID,
MIN(CASE Colum WHEN 'Primary_Kwd' THEN German_Keywords END) Primary_Kwd,
MIN(CASE Colum WHEN 'Sec_Kwd' THEN German_Keywords END) Sec_Kwd,
MIN(CASE Colum WHEN 'Main_Kwd' THEN German_Keywords END) Main_Kwd
FROM CTE3
GROUP BY ID
更新2
关闭CTE3
的括号后,提供以下代码
UPDATE Kwd_UploadRecored
SET Primary_Kwd = TAB.Primary_Kwd,
Sec_Kwd = TAB.Sec_Kwd,
Main_Kwd = TAB.Main_Kwd
FROM
(
SELECT ID,
MIN(CASE Colum WHEN 'Primary_Kwd' THEN German_Keywords END) Primary_Kwd,
MIN(CASE Colum WHEN 'Sec_Kwd' THEN German_Keywords END) Sec_Kwd,
MIN(CASE Colum WHEN 'Main_Kwd' THEN German_Keywords END) Main_Kwd
FROM CTE3
GROUP BY ID
)TAB
WHERE Kwd_UploadRecored.ID=TAB.ID
答案 1 :(得分:0)
工作尝试这个......它会帮助你
DECLARE @MyTable TABLE (id INT IDENTITY(1,1), COLUMN1 VARCHAR(50),coulmn2 VARCHAR(50))
insert into @mytable
select a.column1,a.column2 from Englishgermankwds_tbl as ga outer apply
( select replace(gs.Primary_Kwd ,ga.English_Keywords,ga.German_Keywords) as column1,replace(gs.j,ga.a,ga.j) as column2 from Kwd_UploadRecored as gs)a
select top(select count(*)/4from @mytable)column1,coulmn2 from @mytable where id%2=0