<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: gschro</title>
    <description>The latest articles on DEV Community by gschro (@gschro).</description>
    <link>https://dev.to/gschro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F121689%2F3b0bf850-cde7-4a21-a830-4d5bb1fbd066.jpeg</url>
      <title>DEV Community: gschro</title>
      <link>https://dev.to/gschro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gschro"/>
    <language>en</language>
    <item>
      <title>Conditional Column Join in SQL</title>
      <dc:creator>gschro</dc:creator>
      <pubDate>Fri, 14 Dec 2018 04:19:12 +0000</pubDate>
      <link>https://dev.to/gschro/conditional-column-join-in-sql-5g03</link>
      <guid>https://dev.to/gschro/conditional-column-join-in-sql-5g03</guid>
      <description>&lt;p&gt;Sometimes in the world of legacy enterprise systems you're tasked with what sounds exactly like a problem on &lt;a href="https://app.codesignal.com/" rel="noopener noreferrer"&gt;Code Signal&lt;/a&gt; or similar platforms. The task I ran into today went a little bit like this:&lt;/p&gt;

&lt;p&gt;Given write a query to pull all rows/columns from the tables below where numbers and letters match. If a letter is null include all combinations of colors with shapes in the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5pa53i4lsld9uio8a1m.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5pa53i4lsld9uio8a1m.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing that came to mind was a join of the two tables on the letters and number columns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frph8p8xz69v184jo5fkx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frph8p8xz69v184jo5fkx.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which resulted in:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56up6redadl4ziduv97q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56up6redadl4ziduv97q.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey, the number 1 and letters a and b look good! But where did our number 2's go? Ah yes, the letters are null for 2's in Table1 so they would be dropped when joined to Table2 whose 2's have letters.&lt;/p&gt;

&lt;p&gt;So let's try that again, but forget joining on letters:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1o0hx5rx5iupbs1pqiz6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1o0hx5rx5iupbs1pqiz6.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffz6grydwt8pkrhkiykeq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffz6grydwt8pkrhkiykeq.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yea, the 2's are back! Hmmm, we seem to have a lot of rows...looks like every combination of shape, color, and letter which is great for 2's but not so good for 1's.&lt;/p&gt;

&lt;p&gt;So what we really need is to join on number and letter for 1's and number only for 2's. Enter the conditional column join.&lt;/p&gt;

&lt;p&gt;A conditional column join is a fancy way to let us join to a single column and to two (or more) columns in a single query. We can accomplish this by using a case statement in the on clause of our join.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7fl3yvi8cid6fdxiisp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7fl3yvi8cid6fdxiisp.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A case statement allows us to test multiple conditions (like an if/else if/else) to produce a single value. It's common to choose 1 or 0 (true/false) as the resulting value. &lt;/p&gt;

&lt;p&gt;We then check if the output of the case statement is equal to 1 (the "end = 1" part of the join), if so the join between the two rows takes place, if not the row is dropped.&lt;/p&gt;

&lt;p&gt;So in this case :) the case statement does the following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if Table1's letter column is null.

&lt;ul&gt;
&lt;li&gt;If yes we're going to keep the row (in reality return the value 1 which equals 1). &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If Table1's letter column is not null then check if Table1's letter column equals Table2's letter column. 

&lt;ul&gt;
&lt;li&gt;If yes we keep the row and if not we drop the row. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If neither were true we drop the row (return 0 which does not equal 1).&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Basically all of that let's us join Table1 to the Table2 on just the number column if Table1's letter column is null and join on number and letter if Table1's letter column is not null.&lt;/p&gt;

&lt;p&gt;Which results in our final answer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0wc0p2w3halzytklbrd7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0wc0p2w3halzytklbrd7.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try it out in &lt;a href="http://sqlfiddle.com/#!4/17f36/8" rel="noopener noreferrer"&gt;SQL Fiddle&lt;/a&gt; for some more practice!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
