例如,我将此类数据作为输出
DefectId PMActivityId Date Short_Parameter Unit Value
8 1 2014-07-21 18:20:03.700 WDG. RES. R-Y 1
8 1 2014-07-21 18:20:03.700 WDG. RES. Y-B 2
8 1 2014-07-21 18:20:03.700 WDG. RES. B-R 3
8 1 2014-07-21 18:20:03.700 MEG VAL. R-Y 4
8 1 2014-07-21 18:20:03.700 MEG VAL. Y-B 5
8 1 2014-07-21 18:20:03.700 MEG VAL. B-R 6
8 1 2014-07-21 18:20:03.700 MEG VAL. R-E 7
8 1 2014-07-21 18:20:03.700 MEG VAL. Y-E 8
8 1 2014-07-21 18:20:03.700 MEG VAL. B-E 9
8 1 2014-07-21 18:20:03.700 Abnormality 10
现在我想得到输出为(对于Short_parameter,Unit,Value Column Only DefectId = 8)
WDG.RES.:R-Y:1,WDG.RES。:Y-B:2,WDG.RES.:B-R:3,MEG VAL.:R-Y:4 ......依此类推...... 有可能吗?请指导我。
答案 0 :(得分:0)
我自己设法做到了。
DECLARE @combinedString VARCHAR(MAX)
SELECT
@combinedString = COALESCE(@combinedString + ', ', '') + (Short_Parameter + ':' + ISNULL(Unit,' ') + ':' + Value)
FROM [EDMS_CPL].[dbo].[tblTypeOfActivityValue] WHERE DefectId=8
print @combinedString
答案 1 :(得分:0)
我认为以下代码可能会对您有所帮助
Select STUFF((SELECT ','+(select Short_Parameter+':'+Unit+':'+Value from table WHERE DefectId=8) For XML PATH ('')),1,1,'') as 'xyz'