I'm trying to get a report done within SQL Server that shows each sales campaign that a phone number appears in.
The desired result:
Phonenumber | Area Code | Campaign 1 | Campaign 2 (if applicable) etc...
I have the following code:
with cids as
(
select distinct
a.outbound_cid as [CID],
a.areacode as [AreaCode],
a.campaign_id as [Campaign]
from
vicislave...vicidial_campaign_cid_areacodes a
join
LDS_SALES_CAMPAIGNS b on a.campaign_id = b.campaign_id
)
select *
from cids
order by cid
Which returns this result (data example):
In this example, I would get
2012444340 201 ZEPTR ZEACC ZBEASTC ZEPRR InBnd2 ZEPSC ZEJCC ZEJSC ZEPCC ZEASC
I've not worked with cross-tabbing before, but it appears to be something that may work, but I have not been able to make it work like I can. Obviously, I can do the MIN and MAX of each of these, but I don't know of a way to get the data into the one row, even using things like Excel, without manually doing it, which with 1110 results with these, I don't really have time to do it.
Not to ape the help that I got from below, here is the exact code that worked:
select distinct
a.outbound_cid as [CID],
a.areacode as [AreaCode],
a.campaign_id as [Campaign]
into
#CIDs
from
vicislave...vicidial_campaign_cid_areacodes a
join
LDS_SALES_CAMPAIGNS b on a.campaign_id = b.campaign_id
select distinct
CID,
areacode,
Campaign = stuff((select ', ' + c2.campaign
from #CIDs c2
where c2.CID = c.CID
for xml path(''), type).value('.', 'nvarchar(MAX)'), 1, 1, '')
from
#cids c
drop table #CIDs