Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Use a PIVOT

Status
Not open for further replies.

aliahad

Programmer
Apr 10, 2019
1
US
I need to know how can I use PIVOT , my query

SELECT
A.HIS_STD_OID,
B.HIM_SERIES_ID
FROM
il_cps_conversion.dbo.HEALTH_IMMUNIZATION_SERIES A
INNER JOIN il_cps_conversion.dbo.HEALTH_IMMUNIZATION_DEFINITION B
ON A.HIS_HIM_OID = B.HIM_OID
WHERE
1=1
--AND STDNT.STD_OID = A.HIS_STD_OID
AND A.HIS_FIELDB_001 = '0'
AND A.HIS_STD_OID = 'std01000001256'
AND B.HIM_SERIES_ID <> 'TD'

returns following data set with six rows

Capture1_gbdidr.png


and I need one row with all the six series_id . Like below:

std01000001256: DTP, MMR, MUM, RUB, TDP, VAR

Any suggestion will help.
 
what you need is "for xml path"
Code:
SELECT ID, abc = STUFF
(
    (
        SELECT ',' + name
        FROM temp1 As T2
        -- You only want to combine rows for a single ID here:
        WHERE T2.ID = T1.ID
        ORDER BY name
        FOR XML PATH (''), TYPE
    ).value('.', 'varchar(max)')
, 1, 1, '')
FROM temp1 As T1
GROUP BY id
adapt to your own code as it should be easy and you will possibly learn what it does.

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top