<?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>{Complexity} &#187; Database</title>
	<atom:link href="http://technote.wsjoung.com/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://technote.wsjoung.com</link>
	<description>simple note</description>
	<lastBuildDate>Wed, 09 Nov 2011 02:12:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Calculating distances with geographic data</title>
		<link>http://technote.wsjoung.com/2009/08/17/calculating-distances-with-geographic-data/</link>
		<comments>http://technote.wsjoung.com/2009/08/17/calculating-distances-with-geographic-data/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 20:28:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Distance]]></category>
		<category><![CDATA[Geographic]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://technote.wsjoung.com/?p=97</guid>
		<description><![CDATA[The great circle distance formula: d = acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y2-y1)) * r CREATE TABLE ZCTA ( zcta CHAR(6) NOT NULL , lat_degrees DECIMAL(9,6) NOT NULL , long_degrees DECIMAL(9,6) NOT NULL , PRIMARY KEY(zcta)); ALTER TABLE ZCTA ADD COLUMN lat_radians DECIMAL(9,6) NOT NULL , ADD COLUMN long_radians DECIMAL(9,6) NOT NULL; UPDATE ZCTA SET lat_radians = lat_degrees * (PI() [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>The great circle distance formula:<br />
d = acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y2-y1)) * r</em></strong></p>
<p>CREATE TABLE ZCTA (<br />
zcta CHAR(6) NOT NULL<br />
, lat_degrees DECIMAL(9,6) NOT NULL<br />
, long_degrees DECIMAL(9,6) NOT NULL<br />
, PRIMARY KEY(zcta));</p>
<p>ALTER TABLE ZCTA<br />
ADD COLUMN lat_radians DECIMAL(9,6) NOT NULL<br />
, ADD COLUMN long_radians DECIMAL(9,6) NOT NULL;</p>
<p>UPDATE ZCTA<br />
SET lat_radians = lat_degrees * (PI() / 180)<br />
, long_radians = long_degrees * (PI() / 180);</p>
<p><strong>/* calculating the distance between two points */</strong><br />
SELECT<br />
ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956, 2) AS &#8220;Distance&#8221;<br />
FROM ZCTA x1, ZCTA x2<br />
WHERE x1.zcta = &#8217;1001&#8242;<br />
AND x2.zcta = &#8217;21236&#8242;;</p>
<p><strong>/* zip codes within a given radius */</strong><br />
SELECT<br />
x2.zcta AS zip<br />
, ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956, 2) AS &#8220;Distance&#8221;<br />
FROM ZCTA x1, ZCTA x2<br />
WHERE x1.zcta = &#8217;21236&#8242;<br />
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956 &lt;= 5<br />
ORDER BY Distance;</p>
<p>/* StoreLocation table definition */<br />
CREATE TABLE StoreLocation (<br />
store_id INT NOT NULL AUTO_INCREMENT<br />
, address VARCHAR(100) NOT NULL<br />
, city VARCHAR(35) NOT NULL<br />
, state CHAR(2) NOT NULL<br />
, zip VARCHAR(6) NOT NULL<br />
, PRIMARY KEY (store_id)<br />
, KEY (zip));</p>
<p><strong>/* zip codes within a given radius  with StoreLocation table*/</strong><br />
SELECT<br />
address<br />
, city<br />
, state<br />
, zip<br />
FROM StoreLocation<br />
WHERE zip IN (<br />
SELECT x2.zcta<br />
FROM ZCTA x1, ZCTA x2<br />
WHERE x1.zcta = &#8217;21236&#8242;<br />
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956 &lt;= 5<br />
);</p>
<p>SELECT<br />
address<br />
, city<br />
, state<br />
, zip<br />
FROM StoreLocation s1<br />
INNER JOIN (<br />
SELECT x2.zcta<br />
FROM ZCTA x1, ZCTA x2<br />
WHERE x1.zcta = &#8217;21236&#8242;<br />
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956 &lt;= 5<br />
) AS zips<br />
ON s1.zip = zips.zcta;</p>
<p>SELECT<br />
address<br />
, city<br />
, state<br />
, zip<br />
FROM ZCTA x1, ZCTA x2<br />
INNER JOIN StoreLocation s1<br />
ON x2.zcta = s1.zip<br />
WHERE x1.zcta = &#8217;21236&#8242;<br />
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956 &lt;= 5;</p>
<p>SELECT<br />
address<br />
, city<br />
, state<br />
, zip<br />
, ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956, 2) AS &#8220;Distance&#8221;<br />
FROM ZCTA x1, ZCTA x2<br />
INNER JOIN StoreLocation s1<br />
ON x2.zcta = s1.zip<br />
WHERE x1.zcta = &#8217;21236&#8242;<br />
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)<br />
+ COS(x1.lat_radians) * COS(x2.lat_radians)<br />
* COS(x2.long_radians &#8211; x1.long_radians)) * 3956 &lt;= 5<br />
ORDER BY Distance;</p>
]]></content:encoded>
			<wfw:commentRss>http://technote.wsjoung.com/2009/08/17/calculating-distances-with-geographic-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Style, Theta vs. ANSI</title>
		<link>http://technote.wsjoung.com/2008/04/11/theta-style-vs-ansi-style/</link>
		<comments>http://technote.wsjoung.com/2008/04/11/theta-style-vs-ansi-style/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 21:13:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://wsjoung.wordpress.com/?p=78</guid>
		<description><![CDATA[Theta Style: SELECT i.order_id, p.product_id, p.name, p.desc FROM CustomerItem i, Product p WHERE i.product_id = p.product_id AND i.order_id = 84463; ANSI Style: SELECT i.order_id, p.product_id, p.name, p.desc FROM CustomerItem i INNER JOIN Product p ON i.product_id = p.product_id WHERE i.order_id = 84463; Sometimes, it&#8217;s hard to realized whether there is other style of SQL. Obviously [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Theta Style:</strong></em><br />
SELECT i.order_id, p.product_id, p.name, p.desc<br />
FROM CustomerItem i, Product p<br />
WHERE i.product_id = p.product_id<br />
AND i.order_id = 84463;</p>
<p><em><strong>ANSI Style:</strong></em><br />
SELECT i.order_id, p.product_id, p.name, p.desc<br />
FROM CustomerItem i<br />
INNER JOIN Product p ON i.product_id = p.product_id<br />
WHERE i.order_id = 84463;</p>
<p>Sometimes, it&#8217;s hard to realized whether there is other style of SQL. Obviously there are two different SQL style. theta is older and more obscure but many people still use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://technote.wsjoung.com/2008/04/11/theta-style-vs-ansi-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

