Case - I have
two columns for country code and there are some mutual values and that is totally
fine, but in some cases those columns have individual values for themselves without
the other one having it, which had to be taken care when merging.
Why this
has happened
When I was playing with data as usual I got a scenario where I had two country code columns from two different tables.
When I was playing with data as usual I got a scenario where I had two country code columns from two different tables.
How did I end up here
These columns were extracted from two different tables where one is about offices and the other one is about other places. At one point I had to group by country code and see how many offices per each country and how many other places per each country and above tables are what I got.
These columns were extracted from two different tables where one is about offices and the other one is about other places. At one point I had to group by country code and see how many offices per each country and how many other places per each country and above tables are what I got.
As you can see there is “AE” in both tables but “TH” is only
there in CountryCode/Other table likewise “TW” is only there in
CountryCode/Office table.
What I wanted to do next
Now I want these tables to be merged and have only one column for CountryCode where it should contain all the values from both tables.
My way of
doing it
Since I have two tables first I wanted to merge them and make it one.
Since I have two tables first I wanted to merge them and make it one.
As you can see above I have selected the Merge queries option which is in the top right Home task bar of the query editor. If you don’t understand tap the below task bar and see it for your self.
Do you see it now? Well then I selected "BuildingsOther" which
would have been much nicer if named the other way and merged that
with "BuildingsOffice" and got the following table as result because of the join kind i used "Full Outer".
Full outer join is the best option for you to choose if keeping all the rows is mandatory like the case I'm dealing now.
Full outer join is the best option for you to choose if keeping all the rows is mandatory like the case I'm dealing now.
Now I have two country code columns where
there are some null values in both because of the problem I mentioned in the
very beginning, if you don’t remember the problem scroll to the top.
There may be many ways to solve this but my way of doing is
by using conditional column as shown above.
You can select conditional column from add column task bar of
the query editor. In the below screen shot conditional column is there try to
find it yourself.
Conditional column is
an easy way for you to use an “if condition” even though you don’t have any idea
about it. So good huh, I wanted to name my new column “AllCountryCode” because
I’m going to get all the values in both columns to one single column.
You can see the attributes that I have chosen for the “if
condition” in the above image and the logic I wanted to imply is so and it is below for you.
“Get the values from “CountryCode” if you see a null then
ignore it and put the corresponding value you find in the “BuildingsOfficeCountryCode”
otherwise use the value in the “CountryCode” to fill the “AllCountryCode” column”
If you don’t understand this I highly recommend to read it again.
If you don’t understand this I highly recommend to read it again.
This is what I got for implementing that logic.
Now I have all the country codes in one single column
as I wanted it to be J
Secret
If you have null values for the CountryCode in the individual tables that I have shown in the very beginning this might go wrong when i was implemeting the conditional column but in my case I know there is no null values so my way of doing worked for me.
If you have null values for the CountryCode in the individual tables that I have shown in the very beginning this might go wrong when i was implemeting the conditional column but in my case I know there is no null values so my way of doing worked for me.
Blank and null are two different things. In the “if
condition” I wanted to use “BuildingsOffice.CountryCode” only when “CountryCode”
is null but this won't happen if it is a blank so in that case replacing all blanks by null will solve this like I have shown below.
Comments
Post a Comment