I have a question which is somewhat difficult to put into words, though I hope using a desired output will help to clarify what I am asking.
I have three tables. First table contains CustomerIDs and names:
CREATE TABLE #CustomerTable1
(
CustomerID VARCHAR(25),
CustomerName VARCHAR(25)
)
INSERT INTO #CustomerTable1
VALUES ('0156', 'Frank'), ('0178', 'Darrull'),
('0908', 'Mary'), ('0785', 'Bertha')
CustomerID CustomerName
-------------------------
0156 Frank
0178 Darrull
0908 Mary
0785 Bertha
Second table contains the CustomerID from table 1, a LineNBR column, and what I call an ArbitraryID. A customer can only have one ArbitraryID, but several LineNBRs:
CREATE TABLE #CustomerTable2
(
CustomerIDFromTable1 varchar(25),
LineNBR VARCHAR(25),
ArbitraryID VARCHAR(25)
)
INSERT INTO #CustomerTable2
VALUES ('0156', '1', '167483'), ('0156', '2', NULL),
('0156', '3', NULL), ('0156', '4', NULL),
('0178', '1', NULL), ('0178', '2', '873923'),
('0178', '3', NULL), ('0178', '4', NULL),
('0908', '1', NULL), ('0908', '2', NULL),
('0908', '2', NULL), ('0908', '4', NULL),
('0785', '1', NULL), ('0785', '2', NULL),
('0785', '3', NULL), ('0785', '4', NULL)
CustomerIDFromTable1 LineNBR ArbitraryID
-------------------------------------------
0156 1 167483
0156 2 NULL
0156 3 NULL
0156 4 NULL
0178 1 NULL
0178 2 873923
0178 3 NULL
0178 4 NULL
0908 1 NULL
0908 2 NULL
0908 3 NULL
0908 4 NULL
0785 1 NULL
0785 2 NULL
0785 3 NULL
0785 4 NULL
Third table contains any ArbitraryID from table 2 and an additional ID that I label OtherID. There can only be one OtherID per ArbitraryID:
CREATE TABLE #CustomerTable3
(
ArbitraryIDFromTable2 VARCHAR(25),
OtherID VARCHAR(25)
)
INSERT INTO #CustomerTable3
VALUES ('167483', '89987648'), ('873923', '45564783')
ArbitraryIDFromTable2 OtherID
---------------------------------
167483 89987648
873923 45564783
My question is this:
How do I join these three tables to make sure to get the OtherID, where it exists, for the CustomerIDs that have an a non-null ArbitraryID in table 2?
The results should look like this:
CustomerID CustomerName OtherIDFromTable3
--------------------------------------------------
0156 Frank 89987648
0178 Darrull 873923
0908 Mary NULL
0785 Bertha NULL
I've started with this, but am getting duplicates, of course:
SELECT DISTINCT
a.CustomerID AS CustomerIDFinal,
a.CustomerName,
c.OtherID
FROM
#CustomerTable1 a
LEFT OUTER JOIN
#CustomerTable2 b ON a.CustomerID = b.CustomerIDFromTable1
LEFT OUTER JOIN
#CustomerTable3 c ON c.ArbitraryIDFromTable2 = b.ArbitraryID
EDIT! Just out of curiosity, wondering if someone can solve with an updated version of #CustomerTable2
CREATE TABLE #CustomerTable2
( CustomerIDFromTable1 varchar(25),
LineNBR VARCHAR(25),
ArbitraryID VARCHAR(25))
INSERT INTO #CustomerTable2 VALUES
('0156', '1', '167483'), ('0156', '2', '167483'),
('0156', '3', '167483'), ('0156', '4', '167483'),
('0178', '1', '873923'), ('0178', '2', '873923'),
('0178', '3', '873923'), ('0178', '4', NULL),
('0908', '1', NULL), ('0908', '2', NULL),
('0908', '2', NULL), ('0908', '4', NULL),
('0785', '1', NULL), ('0785', '2', NULL),
('0785', '3', NULL), ('0785', '4', NULL)