我有一个包含单个值和逗号分隔值的列,我想通过匹配描述表来替换它们。
我有一个看起来像这样的表:
文字表示:
Length Directory Extensions Extension Description Type
6684672 Y:\Data\Retail\WalmartMX\Development\Curt.Wolfe\ChristiesAnalysisTool accdb accdb Access Database Development/DB
2002944 Y:\Data\Retail\WalmartMX\Development\SourceCode\WalmartMxDecipher\APPS ACCDB accdb Access Database Development/DB
5312512 Y:\Data\Retail\WalmartMX\Development\SourceCode\AnalysisTool accdb accdb Access Database Development/DB
30986240 Y:\Data\Retail\WalmartMX\Utilities\Tracking\Stats\BAK accdb accdb Access Database Development/DB
112917071 Y:\Data\Retail\WalmartMX\Deploy\Development\SourceCode\WalmartMxDecipher accdb,accde,zip NULL NULL NULL
139053182 Y:\Data\Retail\BQ\Utilities\EMT accdb,bat,docx,laccdb,txt NULL NULL NULL
32116006 Y:\Data\Retail\WalmartMX\Utilities\Tracking\Stats accdb,bat,laccdb,sql,xlsx NULL NULL NULL
我需要替换扩展字段中的值,或者更好地创建一个新字段,其中列表包含来自另一个表的描述。
代码:
SELECT *
FROM [SandboxVinny].[dbo].[FinalDirectoryListing] FDL
left JOIN dbo.SourceExtensions SE
on SE.Extension = FDL.Extensions
显然使用这个,我得到字段中有多个文件扩展名的任何行的空值。
答案 0 :(得分:2)
如何解决:
每行需要一个唯一标识符。
使用rowid和扩展名列创建另一个表
为每个扩展名填充一到多个表格(因此,如果你的示例中rowid从1变为7,那么表格将如下所示:
ID Extension
1 accdb
2 ACCDB
3 accdb
4 accdb
5 accdb
5 accde
5 zip
6 accdb
6 bat
6 docx
6 laccdb
6 txt
7 accdb
7 bat
7 laccdb
7 sql
7 xlsx
现在您的数据是关系型的 - 您可以执行查询并加入此新表,然后加入“其他表”
答案 1 :(得分:1)
It's hard to guess what you want to do, even though you posted some (partial) screenshots.
As Hogan mentioned, the data is hard to query because its structure is not relational. It is possible however. Like this:
SELECT *,
STUFF((SELECT ', ' + se.Description AS [text()]
FROM SourceExtensions AS se
WHERE ',' + fdl.Extensions + ',' LIKE '%,' + se.Extension + ',%'
FOR XML PATH('')), 1, 2, '') AS Description
FROM FinalDirectoryListing AS fdl
This complex FOR XML
trick with a subquery is the SQL Server way of aggregating strings by concatenation. The LIKE
finds the separate extensions in the comma separated list. The STUFF(..., 1, 2, '')
finally removes the ,
before the first element.
Is this what you are looking for?