I have data in a table that looks like this (we'll call it PersonServiceArea for this exercise):
PersonKey ServiceAreaState ServiceAreaCounties
30076 DC * <- Since no counties exist here, this is how we treat the County field
30076 VA ARLINGTON
30076 VA BEDFORD
30076 VA ETC
12345 PA BUCKS
12345 PA CHESTER
12345 PA MONTGOMERY
What I need to do is write something that pulls it together in this format:
PersonKey ServiceAreaState ServiceAreaCounties
30076 DC *
30076 VA ARLINGTON,BEDFORD,ETC
12345 PA BUCKS,CHESTER,MONTGOMERY
However, all I keep coming up with is:
PersonKey ServiceAreaState ServiceAreaCounties
30076 DC *
30076 DC ALEXANDRIA,CITY,ARLINGTON,BEDFORD
30076 DC ALLEGANY,ANNE,ARUNDEL,BALTIMORE
30076 DC BERKELEY,JEFFERSON,MORGAN
30076 DC BRUNSWICK,DUPLIN,NEW,HANOVER
As you can see, my issue is setting up my query so that, for each PersonKey and each individual ServiceAreaState they work in, I only get results back FOR THAT STATE, and not a repeating mess of every county they service under every state they service. Also, counties that consist of two words end up with a comma delimiter as well--how can I avoid this?
For the record, working on this in SQL Server 2008. Any and all help to avoid setting up 100 temporary tables to do the job on a state-by-state basis would be much appreciated!