Here's another possible answer, this one without using a common table expression:
select ContactEmail,
--Note: Current schema allows duplicate email addresses to be active and inactive...
ContactActive = max(ContactActive),
CompanyID = stuff((
select ',' + cast(C2.CompanyID as nvarchar(10))
from Contact C2
where C2.ContactEmail = C1.ContactEmail
group by C2.CompanyID
order by C2.CompanyID
for xml path('')), 1, 1, ''),
Services = stuff((
select ',' + space(1) + ServiceName
from Contact C2
-- Note: This join isn't supported by a single Foreign Key contstraint
join [Service] S on S.CompanyID = C2.CompanyID
where C2.ContactEmail = C1.ContactEmail
group by S.CompanyID, ServiceName
order by S.CompanyID, ServiceName
for xml path('')), 1, 2, '')
from Contact C1
group by ContactEmail
order by CompanyID, min(ContactID)
And for anyone else who wants a crack at this question, here's the setup code I used inferred from the example table data...
if object_id('Contact') is not null drop table Contact;
if object_id('Service') is not null drop table [Service];
if object_id('Company') is not null drop table Company;
go
create table Company (
CompanyID int not null,
CompanyName nvarchar(50) not null,
constraint PK_Company primary key (CompanyID),
constraint ixU_Company_CompanyName unique (CompanyName)
);
insert Company (CompanyID, CompanyName)
values (1, 'CompanyA'),
(2, 'CompanyB'),
(3, 'CompanyC'),
(4, 'CompanyD');
go
create table [Service] (
ServiceID int not null,
ServiceName nvarchar(50) not null,
CompanyID int not null,
constraint PK_Service primary key (ServiceID),
constraint ixU_Service_ServiceName unique (ServiceName),
constraint FK_Service_CompanyID_Company_CompanyID
foreign key (CompanyID)
references Company (CompanyID)
);
insert [Service] (ServiceID, ServiceName, CompanyID)
values (1, 'Electricity', 1),
(2, 'Water', 1),
(3, 'Gas', 2),
(4, 'Lighting', 2),
(5, 'Air Con', 4),
(6, 'Sanitation', 4);
go
create table Contact (
ContactID int not null,
ContactEmail nvarchar(50) not null,
ContactActive nchar(1) not null
constraint DF_Contact_ContactActive default ('Y'),
CompanyID int not null,
constraint PK_Contact primary key (ContactID),
constraint CK_Contact_ContactActive check (ContactActive in ('N', 'Y')),
constraint FK_Contact_CompanyID_Company_CompanyID
foreign key (CompanyID)
references Company (CompanyID)
);
insert Contact (ContactID, ContactEmail, ContactActive, CompanyID)
values (1, 'admin@admin.com', 'Y', 1),
(2, 'admin@admin.com', 'Y', 1),
(3, 'admin1@2.com', 'Y', 2),
(4, 'admin2@2.com', 'Y', 2),
(5, 'admin@3.com', 'Y', 3),
(6, 'admin@4.com', 'Y', 4),
(7, 'accounts@4.com', 'Y', 4),
(8, 'enquiries@4.com', 'Y', 4),
(9, 'services@4.com', 'N', 4),
(10, 'admin@admin.com', 'Y', 4);
go