<?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>Database Lessons &#187; group by</title>
	<atom:link href="http://databaselessons.com/blog/tag/group-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://databaselessons.com/blog</link>
	<description>Tips for MS Access users</description>
	<lastBuildDate>Tue, 19 Jul 2011 11:48:55 +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>How To Do Group By Queries On Groups</title>
		<link>http://databaselessons.com/blog/how-to-do-group-by-queries-on-groups/</link>
		<comments>http://databaselessons.com/blog/how-to-do-group-by-queries-on-groups/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 11:12:17 +0000</pubDate>
		<dc:creator>manxman</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[microsoft access]]></category>
		<category><![CDATA[ms access]]></category>
		<category><![CDATA[Queries]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://databaselessons.com/blog/?p=97</guid>
		<description><![CDATA[I know. Lousy title. Could not dream anything better up at 5:30 am on a Monday morning! A reader wrote in to ask why they could not get their MS Access summary query working. They wanted a report like this. Group Budget Actual 01 $ 34,000 $ 33,756 02 $ 75,000 $ 69,955 03 $ [...]]]></description>
			<content:encoded><![CDATA[<p>I know. Lousy title. Could not dream anything better up at 5:30 am on a Monday morning!</p>
<p>A reader wrote in to ask why they could not get their MS Access summary query working. They wanted a report like this.</p>
<pre>
Group       Budget      Actual
01          $ 34,000    $ 33,756
02          $ 75,000    $ 69,955
03          $ 12,000    $ 13,750
04          $ 25,000    $ 21,223
</pre>
<p>But their report looked like this.</p>
<pre>
Group       Budget      Actual
010250      $ 20,000    $ 20,000
010500      $ 14,000    $ 13,756
020722      $ 20,000    $ 13,455
020999      $ 55,000    $ 56,500
etc.
</pre>
<p>The thing is, that &#8220;Group&#8221; column is based on a BudgetCode field that has values like this.</p>
<p>010250<br />
010500<br />
020722<br />
020999<br />
etc.</p>
<p>The base query that gets us partway there is easily done using the Query Builder. (the steps that I followed below are based on Microsoft Access 2000, but easily translated to other versions)</p>
<ul>
<li>go to the query tab in Access</li>
<li>click on &#8220;New&#8221;</li>
<li>choose Design View and click OK</li>
<li>select the table (or possibly another query) and click Add</li>
<li>click Close</li>
<li>drag the BudgetCode, BudgetAmount, and ActualAmount fields to the query grid</li>
<li>click on the sum symbol (funny looking &#8220;E&#8221; in the toolbar)</li>
<li>under the BudgetAmount and ActualAmount fields in the query grid, click on the Total cell</li>
<li>from the drop down list choose Sum</li>
<li>run the query (use the red exclamation mark in toolbar)</li>
</ul>
<p>That gives us the report style that our reader initially created. With one minor change we will get the final result that we want, summarizing based on just the first 2 characters of the BudgetCode field.</p>
<ul>
<li>go back to design view (a little blue triangle symbol at the far left of the toolbar, or under the &#8220;View&#8221; menu)</li>
<li>change the BudgetCode field name to this (without quotes) &#8211; &#8220;left([BudgetCode],2)&#8221;</li>
<li>rerun the query</li>
</ul>
<p>As a final note, let&#8217;s see what the actual &#8220;code&#8221; looks like. Under the View menu, choose the &#8220;SQL View&#8221;.</p>
<pre>
SELECT Left([BudgetCode],2) AS Expr1,
Sum(data.BudgetAmount) AS SumOfBudgetAmount,
Sum(data.ActualAmount) AS SumOfActualAmount
FROM data
GROUP BY Left([BudgetCode],2);
</pre>
<p>Cool!</p>
<p>As usual, leave comments about your success or questions. Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://databaselessons.com/blog/how-to-do-group-by-queries-on-groups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

