What I need to obtain is to return all rows as columns dynamically and in case Label value exist more than one same value to return like (e.g. Phone, Phone1, Phone2):
For data with isActive=1 and IsPrincipal=1
will be displayed on column without number (e.g. Phone, Fax, E-mail )
create table #Contacts
(
ContactID int,
LabelID int,
Label nvarchar(25),
[Value] nvarchar(25),
IsActive bit,
IsPrimary bit,
CustomerID int
)
insert into #Contacts
values
(1, 1, 'Phone', '(541) 754-3010', 1, 1, 1),
(2, 1, 'Phone', '(541) 764-3011', 1, 1, 2),
(3, 1, 'Phone', '(541) 754-3013', 1, 0, 2),
(4, 1, 'Phone', '(541) 754-3014', 1, 0, 2),
(5, 2, 'Phone personal', '1111 111 1111', 1, 1, 2),
(6, 3, 'Fax', '+44 41 444-5555', 1, 1, 2),
(7, 3, 'Fax', '+44 41 444-5595', 1, 0, 2),
(8, 4, 'E-mail', 'John@sales.com', 1, 0, 1),
(9, 4, 'E-mail', 'office@ExpertBike.com', 1, 1, 1),
(10, 4, 'E-mail', 'Mary@purchasing.com', 1, 0, 1)
create table #Customer (CustomerID int, [Name] nvarchar(30))
insert into #Customer
values
(1, 'Sport Playground'),
(2, 'Expert Bike EU')
What I need to display after joining this tables is shown here:
Customer Name Phone Phone1 Phone2 Phone personal Fax Fax1 E-mail E-mail1 E-mail2
Sport Playground (541) 754-3010 office@ExpertBike.com John@sales.com Mary@purchasing.com
Expert Bike EU (541) 764-3011 (541) 754-3013 (541) 754-3014 1111 111 1111 +44 41 444-5555 +44 41 444-5595
This requirement puts me in trouble.