我有一个具有以下结构的表:
PERSON Sequence TEXT
1 1 John ran
1 2 across the field
2 1 Bill also
2 2 ran across the field after John
我想编写一个产生以下结果的查询:
PERSON TEXT
1 John ran across the field
2 Bill also ran across the field after John
使用T-SQL
可能是这样的答案 0 :(得分:2)
快速草图:
SELECT DISTINCT ST2.person,
SUBSTRING(
(
SELECT ' '+ST1.[text] AS [text()]
FROM [dbo].[tab] ST1
WHERE ST1.person = ST2.person
ORDER BY ST1.person, ST1.sequence
FOR XML PATH ('')
), 2, 1000) AS [text]
FROM [dbo].[tab] ST2
对于任何想要play with it
的人答案 1 :(得分:1)
试试这个
SELECT DISTINCT PERSON, STUFF((SELECT ' ' + TEXT
FROM [YourTable] a
WHERE a.PERSON = b.PERSON
ORDER BY PERSON,SEQUENCE
FOR XML PATH('')),1,1,'') as TEXT
FROM [YourTable] b