<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; non-equi joins</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/non-equi-joins/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:02:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Breaking my Non-Equi Join cherry</title>
		<link>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652</link>
		<comments>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652#comments</comments>
		<pubDate>Wed, 17 Feb 2010 21:56:16 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[non-equi joins]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652</guid>
		<description><![CDATA[There&#8217;s few SQL techniques you seem to keep in the cupboard gathering dust. I don&#8217;t think I&#8217;ve ever needed to use RIGHT JOIN outside of the classroom. I can recall using FULL OUTER JOIN, just once, to show an employer how not-in-sync their &#34;integrated system&#34; was. Today I broke my professional Non-Equi JOIN cherry! I [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652">Breaking my Non-Equi Join cherry</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s few SQL techniques you seem to keep in the cupboard gathering dust. I don&#8217;t think I&#8217;ve ever needed to use <a href="http://en.wikipedia.org/wiki/Join_(SQL)#Right_outer_joins" target="_blank">RIGHT JOIN</a> outside of the classroom. I can recall using <a href="http://en.wikipedia.org/wiki/Join_(SQL)#Full_outer_join" target="_blank">FULL OUTER JOIN</a>, just once, to show an employer how not-in-sync their &quot;integrated system&quot; was. Today I broke my professional <a href="http://www.orafaq.com/wiki/Nonequi_join" target="_blank">Non-Equi JOIN</a> cherry!</p>
<p>I basically had one table of appointments and another table providing appointment banding by a date range.&#160; The banding wasn&#8217;t consistent, spanning weeks and months,&#160; so there was no possibility of using an equi-join or doing anything with datetime arithmetic. Perhaps that non-equi join thing I remember reading in the textbook will do?</p>
<p>Here&#8217;s a quick run-through of a similar situation. Create some tables and insert some test data. (This first example is in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> 2005)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6525"><td class="code" id="p652code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Appointments</span>
<span style="color: #808080;">&#40;</span>
	AppointId <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	AppointmentDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Reason <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> AppointmentBands
<span style="color: #808080;">&#40;</span>
	AppointBandId <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	StartDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	EndDateTime <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #008080;">-- Insert Test Data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Appointments</span>
<span style="color: #808080;">&#40;</span>
	AppointmentDateTime,
	FirstName,
	LastName,
	Reason
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-18T17:00:00'</span>,
	   <span style="color: #FF0000;">'Rhys'</span>,
	   <span style="color: #FF0000;">'Campbell'</span>,
	   <span style="color: #FF0000;">'Eye Test'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-23T12:00:00'</span>,
	   <span style="color: #FF0000;">'John'</span>,
	   <span style="color: #FF0000;">'Smith'</span>,
	   <span style="color: #FF0000;">'Ear Test'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-28T14:00:00'</span>,
	   <span style="color: #FF0000;">'Frank'</span>,
	   <span style="color: #FF0000;">'Zappa'</span>,
	   <span style="color: #FF0000;">'Eye Test'</span>;
GO
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">AppointmentBands</span>
<span style="color: #808080;">&#40;</span>
	StartDateTime,
	EndDateTime
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-18T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-02-22T23:59:59'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-23T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-02-27T23:59:59'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2010-02-28T00:00:00'</span>,
	   <span style="color: #FF0000;">'2010-03-01T23:59:59'</span>;</pre></td></tr></table></div>

<p>I needed to identify which appointment band each record belonged to. It&#8217;s a trivial example here, but the real life situation involved hundreds of thousands of appointments and a few thousand appointment bands. The solution involved using a <a href="http://msdn.microsoft.com/en-us/library/ms187922.aspx" target="_blank">BETWEEN</a> in the join clause.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6526"><td class="code" id="p652code6"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> app.<span style="color: #808080;">*</span>, band.<span style="color: #202020;">AppointBandId</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Appointments</span> app
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">AppointmentBands</span> band
	<span style="color: #0000FF;">ON</span> app.<span style="color: #202020;">AppointmentDateTime</span> <span style="color: #808080;">BETWEEN</span> band.<span style="color: #202020;">StartDateTime</span> <span style="color: #808080;">AND</span> band.<span style="color: #202020;">EndDateTime</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/appointments_non_equi_join.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="appointments non equi join" border="0" alt="appointments non equi join" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/appointments_non_equi_join_thumb.png" width="644" height="188" /></a> </p>
<p>Such a simple, elegant, single query solution. Perhaps we need to dust these things off from time-to-time? </p>
<p>Here&#8217;s the same example, a&#8217;la <a href="http://www.mysql.com" target="_blank">MySQL</a></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code7'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6527"><td class="code" id="p652code7"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> Appointments
<span style="color: #FF00FF;">&#40;</span>
	AppointId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	AppointmentDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	FirstName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	LastName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Reason <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> AppointmentBands
<span style="color: #FF00FF;">&#40;</span>
	AppointBandId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	StartDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	EndDateTime <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Insert Test Data</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> Appointments
<span style="color: #FF00FF;">&#40;</span>
	AppointmentDateTime<span style="color: #000033;">,</span>
	FirstName<span style="color: #000033;">,</span>
	LastName<span style="color: #000033;">,</span>
	Reason
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-18T17:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Rhys'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Campbell'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Eye Test'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-23T12:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'John'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Smith'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Ear Test'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-28T14:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Frank'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Zappa'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'Eye Test'</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> AppointmentBands
<span style="color: #FF00FF;">&#40;</span>
	StartDateTime<span style="color: #000033;">,</span>
	EndDateTime
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-18T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-02-22T23:59:59'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-23T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-02-27T23:59:59'</span>
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'2010-02-28T00:00:00'</span><span style="color: #000033;">,</span>
	   <span style="color: #008000;">'2010-03-01T23:59:59'</span><span style="color: #000033;">;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p652code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6528"><td class="code" id="p652code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> app.<span style="color: #CC0099;">*</span><span style="color: #000033;">,</span> band.AppointBandId
<span style="color: #990099; font-weight: bold;">FROM</span> Appointments app
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> AppointmentBands band
	<span style="color: #990099; font-weight: bold;">ON</span> app.AppointmentDateTime <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> band.StartDateTime <span style="color: #CC0099; font-weight: bold;">AND</span> band.EndDateTime<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/MySQL_non_equi_join.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MySQL non equi join" border="0" alt="MySQL non equi join" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/02/MySQL_non_equi_join_thumb.png" width="644" height="140" /></a></p>
<p><map name='google_ad_map_652_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/652?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_652_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=652&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fbreaking-my-non-equi-join-cherry%2F652' title="Breaking my Non Equi Join cherry" alt=" Breaking my Non Equi Join cherry" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652">Breaking my Non-Equi Join cherry</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/breaking-my-non-equi-join-cherry/652/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
