我有来自4个不同表的查询,我想将其中一个字段值连接到一个字符串,例如我想要下一个查询结果
**BuildName ConfigurationName FileName Name RunIDMax**
Build_20131213.5 Default configuration Screenshot2.png User1 354
Build_20131213.5 Default configuration Screenshot1.png User1 354
将如下所示:
**BuildName ConfigurationName FileName Name RunIDMax**
Build_20131213.5 Default configuration Screenshot2.png, Screenshot1.png User1 354
我有以下问题:
select *
from (select TestPlanName, TestCaseId, AreaPath, IterationPath, TestSuiteSuitePath, ResultTestCaseId, ResultTest, ResultOutcome, max(ResultDate) as MaxResultDate , BuildName, ConfigurationName, FileName, Name, ErrorMessage, max(TA.TestRunId) as RunIDMax
from
[Tfs_Warehouse].[dbo].[DimPerson] (nolock) AS DTRO inner join
[tfs_warehouse].dbo.TestResultView (nolock) a on a.ResultExecutedBySK = PersonSK inner join
[Tfs_DefaultCollection].[dbo].[tbl_Attachment] (nolock) AS TA ON a.TestRunId = TA.TestRunId inner join
[Tfs_DefaultCollection].[dbo].[tbl_TestActionResult] (nolock) AS TAR on TA.TestRunId = Tar.TestRunId
group by TestCaseId,TestPlanName, AreaPath, IterationPath, TestSuiteSuitePath, ResultTestCaseId, ResultTest, ResultOutcome, BuildName, ConfigurationName, FileName, Name, ErrorMessage)
AS b
where
ResultOutcome <> 'Never Run' and (BuildName IN (@BuildName) or @BuildName = null) and (TestPlanName In (@TestPlanName) or @TestPlanName = null)
and TestSuiteSuitePath is not null
and BuildName is not null order by RunIDMax desc
我尝试使用此查询:
select STUFF(
(select ',' + CAST(FileName as nvarchar)
from [Tfs_DefaultCollection].[dbo].[tbl_Attachment]
group by FileName
for xml path('')
),1,1,'')
但我找不到将其集成到主查询中的正确方法。
答案 0 :(得分:0)
尝试集成您的查询,如下所示:
(select STUFF(
(select ',' + CAST(FileName as nvarchar)
from [Tfs_DefaultCollection].[dbo].[tbl_Attachment]
group by FileName
for xml path('')
),1,1,'')) AS FileName
答案 1 :(得分:0)
with TableCTe as
(select 'Build_20131213.5' BuildName, 'Default configuration' ConfigurationName, 'Screenshot2.png' FileName, 'User1' Name, 354 RunIDMax
union all
select 'Build_20131213.5', 'Default configuration', 'Screenshot1.png', 'User1', 354
)
,cte1 as
(
select name,
stuff((select ','+filename from TableCTe for xml path('') ),1,1,'')as filename
from TableCTe
group by name
)
,cte2 as
(select row_number() over(order by a.name )rn, a.BuildName,a.ConfigurationName,a.Name,a.RunIDMax,b.filename from TableCTe a
inner join cte1 b on a.Name=b.Name)
select * from cte2 where rn=1