I am new to SQL and struggling with querying (using Access, FWIW). I have Googled and StackOverflow search, but I have not seen this exact scenario. (This may also be due to the fact that I do not know the correct search conditions.)
I have two pretty simple tables containing similar data.
table1: state, lname, fname, network
table2: state, lname, fname, network
I want to find each person / state combination that matches in two tables, as well as the network from each table that the person is in:
state, lname, fname, t1.network, t2.network.
A person can be in several networks in each table. I want to see each network (from both tables) to which the person belongs.
I started by using JOIN as below:
SELECT t1.state, t1.lname, t1.fname, t1.network, t2.network
FROM t1 INNER JOIN t2
ON t1.fname=t2.fname AND t1.lname=t2.lname AND t1.state=t2.state
GROUP BY t1.state, t1.lname, t1.fname, t1.network, t2.network
I quickly realized that I was getting a Cartesian product. So if "NY, Smith, John" was on two networks in t1 and three networks in t2, I would get something like this:
NY, Smith, John, NetworkA, NetworkB
NY, Smith, John, NetworkA, NetworkA
NY, Smith, John, NetworkB, NetworkA
NY, Smith, John, NetworkB, NetworkB
NY, Smith, John, NetworkA, NetworkC
NY, Smith, John, NetworkB, NetworkC
, , :
NY, Smith, John, NetworkA, NetworkA
NY, Smith, John, NetworkB, NetworkB
NY, Smith, John, NULL, NetworkC
- , ?