<?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>tech stuff &#187; query</title>
	<atom:link href="http://codytaylor.org/tag/query/feed" rel="self" type="application/rss+xml" />
	<link>http://codytaylor.org</link>
	<description>From Cody Taylor.</description>
	<lastBuildDate>Sun, 30 Oct 2011 04:15:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL SUM() Doesn&#8217;t Play Well With Floats</title>
		<link>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html</link>
		<comments>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html#comments</comments>
		<pubDate>Wed, 28 Oct 2009 00:43:11 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[problems and solutions]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[mysql query]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[round]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14302</guid>
		<description><![CDATA[Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn't really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258.]]></description>
			<content:encoded><![CDATA[<p>I had to write some reports for some legacy software today and I was unpleasantly surprised with the results of my SQL queries. I was selecting dollar values and summing them to for the monthly spending of certain individuals. Easy enough right? I wrote a query something like this :<br />
<pre><code>
SELECT SUM(t.money_spent) as sum_of_spent,
c.customer_name 
from transactions t 
join customers c on t.customer_id=c.customer_id 
group by customer_name order by c.customer_name asc
</code></pre><br />
I ended up getting numerical values that were 10 decimal places long with seemingly random numbers. After checking to make sure the database didn&#8217;t have any odd entries I stumbled on this <a href='http://bugs.mysql.com/bug.php?id=1961' title='bug report'>bug report</a>.<br />
The &#8216;money_spent&#8217; column had a data type of float, which is a waste, but I still don&#8217;t think that it should sum up incorrectly. When I select individual values I get proper two decimal results.<br />
Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn&#8217;t really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258. This behavior is a little silly but easily fixed by using the ROUND() function :<br />
<pre><code>
SELECT SUM(ROUND(t.money_spent)) as sum_of_money_spent,
c.customer_name from transactions t 
join customers c on t.customer_id=c.customer_id 
group by customer_name order by c.customer_name asc
</code></pre></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F10%2Fmysql-sum-doesnt-play-well-with-floats.html&amp;linkname=MySQL%20SUM%28%29%20Doesn%26%238217%3Bt%20Play%20Well%20With%20Floats"><img src="http://codytaylor.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codytaylor.org/2009/10/mysql-sum-doesnt-play-well-with-floats.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

