我有一张桌子tb1。我想要连接的结果集。
请帮我写一个关于这个问题的查询?
来,试试这个
SELECT a.dept_id,
NewTable.NameValues
FROM (
SELECT DISTINCT dept_ID
FROM tableA
) a
LEFT JOIN
(
SELECT dept_id,
STUFF((
SELECT ', ' + [Name]
FROM tableA
WHERE ( dept_id = Results.dept_id )
FOR XML PATH('')), 1, 1, '') AS NameValues
FROM tableA Results
GROUP BY dept_id
) NewTable
on a.dept_id = NewTable.dept_id
GO
这是另一个版本
SELECT a.dept_id,
SUBSTRING(d.nameList,1, LEN(d.nameList) - 1) ConcatenateNames
FROM
(
SELECT DISTINCT dept_id
FROM tableA
) a
CROSS APPLY
(
SELECT name + ', '
FROM tableA AS B
WHERE A.dept_id = B.dept_id
FOR XML PATH('')
) D (nameList)
GO
尝试这个:
select col1 + ' ' + col2 from bla.bla.bla