My SQL Fiddle is here: http://sqlfiddle.com/#!3/d5c60
CREATE TABLE customer
(
id int identity primary key,
name varchar(20),
);
CREATE TABLE warehouse
(
id int identity primary key,
name varchar(20),
);
CREATE TABLE customerwarehouse
(
id int identity primary key,
customerid int,
warehouseid int
);
INSERT INTO customer (name)
VALUES
('CustA'),
('CustB'),
('CustC');
INSERT INTO warehouse (name)
VALUES
('wh01'),
('wh02'),
('wh03');
INSERT INTO customerwarehouse (customerid, warehouseid)
VALUES
(1,1),
(2,1),
(2,2),
(3,1),
(3,2),
(3,3);
I would like to write a query to return the customer/warehouse data in the following format:
Customer WH1 WH2 WH3
CustA wh01
CustB wh01 wh02
CustC wh01 wh02 wh03
My attempt to do this returns null for all warehouses.
How can I construct my query to return the data in the required format?