Perry Perry - 11 days ago 7
SQL Question

SQL Combine duplicate columns to 1

I have this query

select im_Call,IM_DR
from cadw.tiburon.inmast m
where IM_DATE < Convert(date,GETDATE()) AND IM_DATE >= DATEADD(DD, - 4,Convert(date,GETDATE()))


which returns results similar to this

enter image description here

I need to take the records that are highlited in yellow so I get results that would return 1 row with the im_Call number with the IM_DR column showing both of the highlighted records as 1 row with 2 columns like this

im_Call IM_DR
163220023 160104238, 160104238

Answer

Perhaps this can help

Edit- Added the CTE to keep the scope of the original query

 ;with cte as (
    Select im_Call,IM_DR
    from cadw.tiburon.inmast m
    where IM_DATE < Convert(date,GETDATE()) AND IM_DATE >= DATEADD(DD, - 4,Convert(date,GETDATE()))
) 
 Select IM_Call
       ,IM_DR = (Select Stuff((Select Distinct ',' +cast(IM_DR as varchar(25)) From cte Where IM_Call=A.IM_Call For XML Path ('')),1,1,'') )
 From (Select Distinct IM_Call From cte) A
Comments