我的表格
MyTable的
+----+-------+---------------+
| Id | Title | DependencyIds |
+----+-------+---------------+
DependentIds
包含14;77;120
等值。
MyDependentTable
+--------------+------+
| DependencyId | Name |
+--------------+------+
背景
我必须从MyTable
中选择数据,其中MyDependentTable
的所有依赖项都以逗号分隔。
预期产出:
+---------+-------------------------------------+
| Title | Dependencies |
+---------+-------------------------------------+
| Test | ABC, One-two-three, Some Dependency |
+---------+-------------------------------------+
| Example | ABC |
+---------+-------------------------------------+
我的查询
SELECT t.Title,
(SELECT ISNULL((
SELECT DISTINCT
(
SELECT dt.Name + '',
CASE WHEN DependencyIds LIKE '%;%' THEN ', ' ELSE '' END AS [text()]
FROM MyDependentTable dt
WHERE dt.DependencyId IN (SELECT Value FROM dbo.fSplitIds(t.DependencyIds, ';'))
ORDER BY dt.DependencyId
FOR XML PATH('')
)), '')) Dependencies
FROM dbo.MyTable t
问题描述
查询有效,但在存在多个依赖项时会添加一个额外的逗号:
+---------+---------------------------------------+
| Title | Dependencies |
+---------+---------------------------------------+
| Test | ABC, One-two-three, Some Dependency, |
+---------+---------------------------------------+
| Example | ABC |
+---------+---------------------------------------+
我无法使用SUBSTRING(ISNULL(...
,因为我无法访问字符串的长度,因此我无法设置SUBSTRING
的长度。< / p>
有没有可能摆脱那些不必要的额外逗号?
答案 0 :(得分:2)
通常,对于Sql Server中的组连接,人们会添加前导逗号并使用STUFF
函数将其删除,但即使这看起来很丑陋。
Outer Apply
方法看起来很干净,而不是correlated sub-query
。在此方法中,我们不必使用SELECT
或ISNULL
STUFF
查询
SELECT DISTINCT t.title,
Isnull(LEFT(dependencies, Len(dependencies) - 1), '')
Dependencies
FROM dbo.mytable t
OUTER apply (SELECT dt.NAME + ','
FROM mydependenttable dt
WHERE dt.dependencyid IN (SELECT value
FROM
dbo.Fsplitids(t.dependencyids,';'))
ORDER BY dt.dependencyid
FOR xml path('')) ou (dependencies)
答案 1 :(得分:0)
以下是使用STUFF
的方法。
SELECT t.Title
,STUFF((SELECT ', ' + CAST(dt.Name AS VARCHAR(10)) [text()]
FROM MyDependentTable dt
WHERE dt.DependencyId IN (SELECT Value FROM dbo.fSplitIds(t.DependencyIds, ';'))
ORDER BY dt.DependencyId
FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,2,' ') Dependencies
FROM dbo.MyTable t