我有一个分为多行的表。该表如下所示:
表admissions
:
PersonID Field_Value
------------------------
1775 UNDECIDED
1775 PC-STJ
1775 N/A
1775 DEMENTIA
1775 STJ
1775 0
1775 N/A
1775 N/A
1775 N
我有一个视图将捕获如下所示的一行信息。
请注意,以上是它在数据库中为我提供的与UI不同的内容。
1775, UNDECIDED, PC -STJ, N/A, DEMENTIA, STJ, 0, N/A, N/A, N
答案 0 :(得分:0)
试试这个:
SELECT o.PersonID , fieldvalues = STUFF(
(SELECT ',' + field_value
FROM admissions i
where i.PersonID= o.PersonID
FOR XML PATH (''))
, 1, 1, '')
from admissions o. Group by o.PersonID ;
答案 1 :(得分:0)
DECLARE @Admission TABLE (PersonID INT, Field_Value VARCHAR(25));
INSERT INTO @Admission
VALUES (1775,'UNDECIDED'),
(1775,'PC-STJ'),
(1775,'N/A'),
(1775,'DEMENTIA'),
(1775,'STJ'),
(1775,'0'),
(1775,'N/A'),
(1775,'N/A'),
(1775,'N');
SELECT PersonID,
STUFF(
(
SELECT ', ' + Field_Value
FROM @Admission
WHERE PersonID = A.PersonID
FOR XML PATH('')
),
1,2,'') AS Field_Values
FROM @Admission A
GROUP BY PersonID
结果:
PersonID Field_Values
----------- ------------------------------------------------------
1775 UNDECIDED, PC-STJ, N/A, DEMENTIA, STJ, 0, N/A, N/A, N