0

I have two datasets: A and B

Dataset A:

indiv_id January 1000068000004 1 1000068000011 1 1000068000012 1 

Dataset B:

indiv_id March 1000068000003 1 1000068000011 1 1000068000015 1 

Desired Result:

Indiv_id January March 1000068000003 null 1 1000068000004 1 null 1000068000011 1 1 1000068000012 1 null 1000068000015 null 1 

How do I join the tables so that I have all of the Indiv_IDs from BOTH lists and their values in the column that is distinct to their particular dataset?

Thanks

3
  • Show us some sample table data and the expected result - all as formatted text, not images. And take a look at stackoverflow.com/help/mcve! Commented Mar 11, 2019 at 13:34
  • yeah otherwise we don't know what you want when you have the same indiv_ID in both tables - a single combined line or two separate lines? Commented Mar 11, 2019 at 13:36
  • I've updated. Thanks Commented Mar 11, 2019 at 13:41

2 Answers 2

2

You can use full join:

select indiv_id, a.january, b.march from a full join b using (indiv_id); 

The using clause makes this particularly convenient because you don't need to worry about any coalesce()s in the select.

Sign up to request clarification or add additional context in comments.

1 Comment

That USING tidbit is a nice bonus. I'd never realized that.
0

You've got 3 cases to deal with: the ID is in both datasets, the ID is only in dataset A, or the ID is only in dataset B. You can't solve that with select ... from dataset_A left join dataset_B ... because you'll get the first two cases but not the third.

What you need is to define a view that gives you all the IDs across both datasets, then left join to each dataset:

with all_IDs as (select indiv_ID from dataset_A union select indiv_ID from dataset_B) select indiv_ID, january, march from all_IDs left join dataset_A using (indiv_ID) left join dataset_B using (indiv_ID) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.