<?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>TechShorts &#187; SQL Origami</title>
	<atom:link href="http://blog.ddpruitt.net/category/sql-origami/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ddpruitt.net</link>
	<description>Improving the development process one day at a time.</description>
	<lastBuildDate>Wed, 28 Jan 2009 18:10:00 +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>SQL Moment of Clairity</title>
		<link>http://blog.ddpruitt.net/2006/10/17/sql-moment-of-clairity/</link>
		<comments>http://blog.ddpruitt.net/2006/10/17/sql-moment-of-clairity/#comments</comments>
		<pubDate>Wed, 18 Oct 2006 01:16:59 +0000</pubDate>
		<dc:creator>darren</dc:creator>
				<category><![CDATA[SQL Origami]]></category>

		<guid isPermaLink="false">http://blog.ddpruitt.net/2006/10/17/sql-moment-of-clairity/</guid>
		<description><![CDATA[I was working on a stored procedure the other day, not one that I wrote originally but one that was causing performance issues in out system.  The procedure was taking any where from 40 to 60 seconds to run, which is unacceptable in a web service.  This in fact was causing the web [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a stored procedure the other day, not one that I wrote originally but one that was causing performance issues in out system.  The procedure was taking any where from 40 to 60 seconds to run, which is unacceptable in a web service.  This in fact was causing the web service to time out.</p>
<p>An inspection of the execution plan showed that one part of the procedure was doing an index scan and that it was this that was taking up over 85% of the execution time.  I was totally baffled, it was hitting the index so why wasn’t it faster?</p>
<p>Then I learned three things all at once:</p>
<ul>
<li>Implicit Conversion</li>
<li>non-sargable kills performance</li>
<li>Index Seek is preferred of Index Scan</li>
</ul>
<p><strong>Implicit Conversion</strong></p>
<p>In the stored proc a variable was defined as an NVARCHAR(20) but the field in the table it was being compared to was a CHAR(10).  This lead to an implicit conversion of the variable to a CHAR(10), which lead into the next issue:</p>
<p><strong>Non-Sargable Kills Performance</strong></p>
<p>Sargable refers to the pseudo-acronym “SARG” – Search ARGument and refers to a WHERE clause that compares a constant value  to a column value.  The implicit conversion was causing a non-SARGable condition which means the WHERE clause cannot use an index.</p>
<p><strong>Index Scan</strong></p>
<p>Because of the non-sargable condition an Index Scan was being performed.  An Index Scan is just as bad as a Table Scan in the SQL realm and should be avoided at all costs.</p>
<p>The solution was simple:  Change the variable to a CHAR(10).  After doing that the Index Scan became an Index Seek and the whole stored procedure returned in less than a second.  Any time I see an order of magnitude improvement like that from one simple change it just boggles my mind.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ddpruitt.net/2006/10/17/sql-moment-of-clairity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Period Matrix</title>
		<link>http://blog.ddpruitt.net/2004/09/25/period-matrix/</link>
		<comments>http://blog.ddpruitt.net/2004/09/25/period-matrix/#comments</comments>
		<pubDate>Sat, 25 Sep 2004 22:01:24 +0000</pubDate>
		<dc:creator>darren</dc:creator>
				<category><![CDATA[SQL Origami]]></category>

		<guid isPermaLink="false">http://blog.ddpruitt.net/?p=6</guid>
		<description><![CDATA[The Period Matrix table, Pd_Matrix, is used in calculating accounting period formulas. These formulas include current month, quarter or years activity or balance. These formulas can also be created using standard SQL functions but not all functions are common between SQL Server and MS Access. The Period Matrix has the advantage in that it can [...]]]></description>
			<content:encoded><![CDATA[<p>The Period Matrix table, Pd_Matrix, is used in calculating accounting period formulas. These formulas include current month, quarter or years activity or balance. These formulas can also be created using standard SQL functions but not all functions are common between SQL Server and MS Access. The Period Matrix has the advantage in that it can be used in any DBMS and it also can be used in table pivoting and folding. </p>
<p><span id="more-6"></span></p>
<p>The table listed below is for use with the Activity table and not the Balance table. Either a separate matrix table will have to be created or an additional field would have to be added for calculating account formulas with the Balance table.</p>
<table style="BACKGROUND: #fff" cellSpacing=1 cellPadding=1 border=1>
<thead>
<tr style="BORDER-RIGHT: black solid; BORDER-TOP: black solid; BACKGROUND: silver; BORDER-LEFT: black solid; BORDER-BOTTOM: black solid">
<td>Pd_Type</td>
<td>Pd_ID</td>
<td>Pd_Name</td>
<td>pd_1</td>
<td>pd_2</td>
<td>pd_3</td>
<td>pd_4</td>
<td>pd_5</td>
<td>pd_6</td>
<td>pd_7</td>
<td>pd_8</td>
<td>pd_9</td>
<td>pd_10</td>
<td>pd_11</td>
<td>pd_12</td>
</tr>
</thead>
<tbody>
<tr vAlign=top>
<td>annual </td>
<td>1 </td>
<td>YR </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>1 </td>
<td>JAN </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>2 </td>
<td>FEB </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>3 </td>
<td>MAR </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>4 </td>
<td>APR </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>5 </td>
<td>MAY </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>6 </td>
<td>JUN </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>7 </td>
<td>JUL </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>8 </td>
<td>AUG </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>9 </td>
<td>SEP </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>10 </td>
<td>OCT </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>11 </td>
<td>NOV </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>month </td>
<td>12 </td>
<td>DEC </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
</tr>
<tr vAlign=top>
<td>quarter </td>
<td>1 </td>
<td>QTR1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>quarter </td>
<td>2 </td>
<td>QTR2 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>quarter </td>
<td>3 </td>
<td>QTR3 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>quarter </td>
<td>4 </td>
<td>QTR4 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
</tr>
<tr vAlign=top>
<td>semiannual </td>
<td>1 </td>
<td>SA1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
</tr>
<tr vAlign=top>
<td>semiannual </td>
<td>2 </td>
<td>SA2 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>0 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
<td>1 </td>
</tr>
</tbody>
</table>
<h3>Pd_Type</h3>
<p>The Period Type is used to indicate the type of formula being used. The types listed are:</p>
<ul>
<li>
<p>Month: The given months activity</p>
<li>
<p>Quarter: The given quarters activity</p>
<li>
<p>Semiannual: The given half-year activity</p>
<li>
<p>Annual: The entire years activity</p>
<li>
<p>Other types that could be created include a months or quarter ending balance.</p>
</li>
</li>
</li>
</li>
</li>
</ul>
<h3>The Period ID and Name are used to identify a given time frame:</h3>
<ul>:&nbsp;The month number (1 thru 12) and name (January thru December)
<li>Quarter:&nbsp;The quarter number (1 thru 4) and name (QTR1 thru QTR4)</li>
<li>Semiannual:&nbsp;The semiannual number (1 or2) and name (SA1 and SA2)</li>
<li>Annual:&nbsp;The year number (1) or name (YR)</li>
</ul>
<h3>pd_1 thru pd_12</h3>
<p>The values for period 1 through 12 can only be 1 or 0. These fields are multiplied with the Activity table month fields to create the necessary formulas.</p>
<h2>Creating Formulas</h2>
<p>To create a formula using the Period Matrix and the Activity table you would create a select statement where in the period fields in the Activity table were multiplied to the period fields in the Matrix table then add them all together:</p>
<p>( Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12)</p>
<p>In the WHERE clause of the select statement you would set the Period Type (Pd_Type) and Period Name (Pd_Name) of the Matrix table to the name of the formula. For example, if you wanted to show the monthly activity for January you would create the following select statement:</p>
<p>SELECT<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Class<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year<br />&nbsp;&nbsp;&nbsp;&nbsp; , ( Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) As Amount</p>
<p>FROM Activity, Pd_Matrix</p>
<p>WHERE<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (Pd_Type = &#8216;month&#8217;)<br />&nbsp;&nbsp;&nbsp;&nbsp; And (Pd_Matrix.Pd_Name = &#8216;JAN&#8217;)</p>
<p>This SQL statement could be changed to a parameterized stored procedure with variables for Pd_Type and Pd_Name. This would allow you to return a recordset for any of the given formulas.</p>
<h2>Table Folding</h2>
<p>The Period Matrix can also be used in table folding, where the columns of the original table are converted to rows. This is accomplished by creating a Cartesian product between the original table, in our case the Activity table, and the Period Matrix table.</p>
<p>A Cartesian product result set, also known as a Cross Join, is familiar to people that have created SQL statements with bad or missing joins between tables. They are recognizable because the result set will have the same row multiple times or the total row count far exceeds the total number of expected rows. In most cases this is unwanted but it comes in very handy for table folding.</p>
<p>The following SQL select statement will fold the Activity table&#8217;s period activity columns into rows:</p>
<p>SELECT</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Class<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year<br />&nbsp;&nbsp;&nbsp;&nbsp; ,&nbsp;&nbsp;&nbsp; (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) As Amount</p>
<p>FROM Activity, Pd_Matrix<br />&nbsp;<br />WHERE Pd_Type = &#8216;month&#8217;</p>
<p>Note that this is identical to the SQL used to show the activity for just one month used previously, with the only exception being in the WHERE clause. Instead of indicating which month (Pd_Name) to show, the SQL was allowed to return all the available months.</p>
<p>This statement works for the same reasons the previous SQL statement worked. When the Pd_Name is JAN then pd_1 equals one while the rest of the pd_2 through pd_12 equals zero. When the multiplications are carried out all the results are all zero except for January&#8217;s, which is equal to the January activity. When the addition is carried out therefore only the January amount is returned.</p>
<p>Also, since there is no join between the two tables the result set will have twelve times the number of rows that the Activity table actually has. For each row in the Activity table there will be twelve rows generated in the result because there are twelve possible rows for the Pd_Type of &#8216;month&#8217;. If instead of choosing &#8216;month&#8217; we had chosen &#8216;quarter&#8217; for the Period Type then the results set would have four times the number of rows in the Activity table.</p>
<p>This method of table folding is fast and very simple to create and maintain. The most common way table folding is done though is by manually creating the final table then creating an append or update query for each column to fold. In the case of our example we would have had to create and execute thirteen queries in order to obtain the same results.</p>
<h2>Table Folding and Pivoting Using Formulas</h2>
<p>Where as table folding is deals with turning columns into rows, table pivoting is turning rows into columns.</p>
<p>Taking the example from the table folding section lets add the requirement that our resultset show the Actual and Budget amounts in separate columns. The periods will still be shown as rows.</p>
<p>The SQL needed in Access would be:</p>
<p>SELECT
<p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year<br />&nbsp;&nbsp;&nbsp;&nbsp; ,Sum( IIF(Activity.Class = &#8220;Actual&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ), 0)) As Actual</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; ,Sum( IIF(Activity.Class = &#8220;Budget&#8221;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ), 0)) As Budget</p>
<p>FROM Activity, Pd_Matrix</p>
<p>WHERE Pd_Type = &#8216;month&#8217;</p>
<p>GROUP BY<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year</p>
<p>In SQL Server the CASE statement would replace the IIF( ) function:</p>
<p>SELECT
<p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; ,Sum( CASE Activity.Class<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN &#8220;Actual&#8221;THEN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) ELSE 0 END) Actual</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; ,Sum( CASE Activity.Class<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN &#8220;Budget&#8221;THEN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) ELSE 0 END) Budget</p>
<p>FROM Activity, Pd_Matrix</p>
<p>WHERE Pd_Type = &#8216;month&#8217;<br />&nbsp;<br />GROUP BY<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Activity.AcctID<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.DeptID<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; , Pd_Matrix.Pd_Name<br />&nbsp;&nbsp;&nbsp;&nbsp; , Activity.Year<br />&nbsp;<br />
<h2>Summary</h2>
<p>The Period Matrix table is a versatile tool for SQL development. It can be used in all DBMSs with little modification to the SQL statements. It can be used to replace several separate SQL statements needed to create the different formulas and it is useful in table folding and pivoting.</p>
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ddpruitt.net/2004/09/25/period-matrix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MS Access Pivots Part II</title>
		<link>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-ii/</link>
		<comments>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-ii/#comments</comments>
		<pubDate>Sat, 25 Sep 2004 21:45:45 +0000</pubDate>
		<dc:creator>darren</dc:creator>
				<category><![CDATA[SQL Origami]]></category>

		<guid isPermaLink="false">http://blog.ddpruitt.net/?p=4</guid>
		<description><![CDATA[Pivoting a table on multiple columns with a crosstab query gets messy really fast.&#160; To eliminate the need for complicated Multiple Value Field Queries you can use the lowly IIF() function and the obscure CHOOSE() function to pivot a table.

Activity Table
The table Activity used in this example has the following layout:



AcctID 
DeptID 
Class 
Year 
BeginBalance [...]]]></description>
			<content:encoded><![CDATA[<p>Pivoting a table on multiple columns with a crosstab query gets messy really fast.&nbsp; To eliminate the need for complicated Multiple Value Field Queries you can use the lowly IIF() function and the obscure CHOOSE() function to pivot a table.</p>
<p><span id="more-4"></span></p>
<h3>Activity Table</h3>
<p>The table Activity used in this example has the following layout:</p>
<table style="WIDTH: 649px" borderColor=#000000 cellSpacing=1 cellPadding=3 border=1>
<tbody>
<tr>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="11%" height=22>AcctID </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="11%" height=22>DeptID </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="9%" height=22>Class </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="8%" height=22>Year </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="19%" height=22>BeginBalance </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>Jan </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>Feb </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>Mar </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>Apr </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>May </td>
<td style="BACKGROUND-COLOR: rgb(192,192,192)" vAlign=top width="7%" height=22>Jun</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Jul</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Aug</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Sep</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Oct</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Nov</td>
<td style="VERTICAL-ALIGN: top; BACKGROUND-COLOR: rgb(192,192,192)">Dec</td>
</tr>
<tr>
<td vAlign=top width="11%" bgColor=#ffffff height=22></td>
<td vAlign=top width="11%" bgColor=#ffffff height=22></td>
<td vAlign=top width="9%" bgColor=#ffffff height=22></td>
<td vAlign=top width="8%" bgColor=#ffffff height=22></td>
<td vAlign=top width="19%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td vAlign=top width="7%" bgColor=#ffffff height=22></td>
<td style="VERTICAL-ALIGN: top"></td>
<td style="VERTICAL-ALIGN: top"></td>
<td style="VERTICAL-ALIGN: top"></td>
<td style="VERTICAL-ALIGN: top"></td>
<td style="VERTICAL-ALIGN: top"></td>
<td style="VERTICAL-ALIGN: top"></td>
</tr>
</tbody>
</table>
<p>The AcctID and DeptID refer to account and department numbers, which are a part of our fictitious company’s accounting key. The Class refers to the type of monies, in our examples we will stick to only Actual and Budget dollar amounts. The Year is the calendar year in which the values apply. The BeginBalance is the current Year’s beginning balance while the fields Jan through Dec each contain the activity for the given month.</p>
<p>The data in the table was generated using the following criteria:</p>
<ol>
<li>The current calendar year is 1999.</li>
<li>The current accounting period is June.</li>
<li>There are two years of history, so years 1997 and 1998 have actual amounts only.</li>
<li>There is a budget for years 1999 and 2000.</li>
<li>There are no actual amounts for the year 2000, only budget amounts.</li>
</ol>
<h3>Budget Variance and History</h3>
<p>In our first example we will show two techniques of producing a very common accounting scenario: generating a report for a given accounting period showing the comparison between the actual and budgeted amounts, along with history. As an added twist we will show the next years budget also. For this example we will further limit it to show January 1999 as the current accounting period.</p>
<p>The result we produce will need to show the account-department number, January 1997, 1998 and 1999 actual amounts, January 1999 and 2000 budget amounts.</p>
<p>As can be seen, we are pivoting the Actual table on two fields, Year and Class. We will employee several techniques to perform this pivoting to show that there is more than one way to do it. The techniques we will use are:</p>
<ul>
<li>Select query with the IIF( ) function</li>
<li>Select query with a combination of the IIF( ) and CHOOSE( ) functions</li>
</ul>
<h3>
<p>Pivot using IIF( ) Function</p>
</h3>
<p>The IIF() function is a built in MS Access function. It is identical to the IF() function in Excel and has the following syntax:</p>
<p><dir>
<pre>IIF( [logical test], [value if true], [value if false] )</pre>
<p></dir>
<p>The logical test can be any type of expression that returns a true or false value. </p>
<p>As a simple example, say we wanted to select the Class field from the Activity table only the word &#8220;actual&#8221;. If the Class is &#8220;actual&#8221; we want to see &#8220;Real Dollars&#8221;, otherwise show &#8220;Not Real Dollars&#8221;. To do this we would create the following query:</p>
<p><dir>
<pre>SELECT Activity.AcctID, Activity.DeptID, IIf([Class]="actual","Real Dollars","Not Real Dollars") AS RealDollars, Activity.BeginBalanceFROM Activity;</pre>
<p></dir>
<p>This would return the beginning balances with the either &#8220;Real Dollars&#8221; or &#8220;Not Real Dollars&#8221; in the RealDollars field. An example output is:</p>
<table borderColor=#000000 cellSpacing=1 cellPadding=3 width=383 border=1>
<thead>
<tr>
<td vAlign=top width="16%" bgColor=#c0c0c0 height=22>
<p align=center>AcctID</p>
</td>
<td vAlign=top width="17%" bgColor=#c0c0c0 height=22>
<p align=center>DeptID</p>
</td>
<td vAlign=top width="36%" bgColor=#c0c0c0 height=22>
<p align=center>RealDollars</p>
</td>
<td vAlign=top width="31%" bgColor=#c0c0c0 height=22>
<p align=center>BeginBalance</p>
</td>
</tr>
</thead>
<tbody>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$2,530.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$8,690.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$8,235.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Not Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$5,005.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="17%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="36%" bgColor=#ffffff height=22>
<p>Not Real Dollars</p>
</td>
<td vAlign=top width="31%" bgColor=#ffffff height=22>
<p align=right>$9,470.00</p>
</td>
</tr>
</tbody>
</table>
<p>Now let us turn our attention back to the pivot table problem. The problem is to show for the years 1997 through 1999 the actual dollar amounts (Class = actual), and for the years 1999 and 2000 the budgeted amounts (Class = budget).</p>
<p>Looking at the data, we know that for the years 1997 and 1998 there are no budget amounts. Because of this we can set up two fields in our query using the IIF( ) function and the table field [Year]:</p>
<p><dir>
<pre>[1997] = IIF( [Year] = "1997", [Jan], 0)[1998] = IIF( [Year] = "1998", [Jan], 0)</pre>
<p></dir>
<p>With these two functions the given query field value will either be the given years January actual amount or zero.</p>
<p>The next two years, 1999 and 2000, have both actual and budget amounts. However, since we are currently in the calendar year 1999 the year 2000 will have only budget amounts. Using this we can create fields for 1999 and 2000:</p>
<p><dir>
<pre>[Actual 1999] = IIF ([Year]="1999" AND [Class] = "actual", [Jan], 0)[Budget 1999] = IIF ([Year]="1999" AND [Class] = "budget", [Jan], 0)[Budget 2000] = IIF ([Year]="2000" AND [Class] = "budget", [Jan], 0)</pre>
<p></dir>
<p>These functions will return their indicated values, January Actual 1999, Budget 1999 or Budget 2000, or they will return a zero.</p>
<p>Combining these into a final select query we get:</p>
<p><dir>
<pre>SELECT act.AcctID, act.DeptID, IIf([act].[Year]="1997",[act].[Jan],0) AS 1997, IIf([act].[Year]="1998",[act].[Jan],0) AS 1998, IIf([act].[Year]="1999" And [act].[Class]="actual",[act].[Jan],0) AS Actual1999, IIf([act].[Year]="1999" And [act].[Class]="budget",[act].[Jan],0) AS Budget1999, IIf([act].[Year]="2000" And [act].[Class]="actual",[act].[Jan],0) AS Actual2000, IIf([act].[Year]="2000" And [act].[Class]="budget",[act].[Jan],0) AS Budget2000FROM Activity AS act;</pre>
<p></dir>
<p>As a result for account 1000 we get:</p>
<table borderColor=#000000 cellSpacing=1 cellPadding=3 width=647 border=1>
<tbody>
<tr>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>AcctID</p>
</td>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>DeptID</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1997</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1998</p>
</td>
<td vAlign=top width="16%" bgColor=#c0c0c0 height=22>
<p align=center>Actual1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget2000</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$1,955.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$3,925.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,195.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$3,575.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,470.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,540.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,090.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,620.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,980.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$6,145.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$9,285.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$1,820.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$2,340.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,110.00</p>
</td>
</tr>
</tbody>
</table>
<p>As you can see there are a lot of zeros. It almost looks like we accomplished the goal of the problem. We have columns with only 1997 values, or 1998 values, or Actual 1999 values, etc, which means we have successfully pivoted on the desired fields, but this result set is quit useless as it is. What we need now is a way to reduce the rows down so that true comparisons can be made between the history and the budget.</p>
<p>This is accomplished by using the aggregate function sum() in the query. Rewriting the query and summing on the amount columns we generate the following select query:</p>
<p><dir>
<pre>SELECT act.AcctID, act.DeptID, Sum(IIf([act].[Year]="1997",[act].[Jan],0)) AS 1997, Sum(IIf([act].[Year]="1998",[act].[Jan],0)) AS 1998, Sum(IIf([act].[Year]="1999" And[act].[Class]="actual",[act].[Jan],0)) AS Actual1999, Sum(IIf([act].[Year]="1999" And[act].[Class]="budget",[act].[Jan],0)) AS Budget1999, Sum(IIf([act].[Year]="2000" And[act].[Class]="budget",[act].[Jan],0)) AS Budget2000FROM Activity AS actGROUP BY act.AcctID, act.DeptID;</pre>
<p></dir>
<p>This query returns the results we can use:</p>
<table borderColor=#000000 cellSpacing=1 cellPadding=3 width=647 border=1>
<tbody>
<tr>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>AcctID</p>
</td>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>DeptID</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1997</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1998</p>
</td>
<td vAlign=top width="16%" bgColor=#c0c0c0 height=22>
<p align=center>Actual1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget2000</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$1,955.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$3,925.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,195.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$3,575.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,470.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,540.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,090.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,620.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,980.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$6,145.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$9,285.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$1,820.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$2,340.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,110.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2020</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,460.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$5,005.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$8,825.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,000.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,500.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>3000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$4,605.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,355.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$2,265.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$6,115.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,050.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>3010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$6,715.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,665.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$5,390.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$695.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,885.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>4000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$540.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$9,085.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$7,775.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,550.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,255.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>4010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$7,885.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$1,035.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$2,870.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$1,265.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
</tr>
</tbody>
</table>
<p>By grouping on the AcctID and DeptID and summing on the other columns we have effectively pivoted the table on two columns, [Year] and [Class]. </p>
<p>Looking at the first result set (without the sum()) we see that for AcctID 1000, DeptID 1000 there is one row for each non-zero column value (this is assuming that no original value was zero to begin with). What is not seen is the fact that this result is really a snap shot of the original table but without showing the [Year] and [Class] columns, these are hidden in the IIF( ) function.</p>
<p>By comparing the original table (below) to the two above we can see how the values where first pivoted on the [Year] field (first query) then summed to pivot on the [Class] field.</p>
<table borderColor=#000000 cellSpacing=1 cellPadding=3 width=329 border=1>
<tbody>
<tr>
<td vAlign=top width="19%" bgColor=#c0c0c0 height=22>
<p align=center>AcctID</p>
</td>
<td vAlign=top width="20%" bgColor=#c0c0c0 height=22>
<p align=center>DeptID</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Class</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>Year</p>
</td>
<td vAlign=top width="28%" bgColor=#c0c0c0 height=22>
<p align=center>Jan</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1997</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$1,955.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1998</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$3,925.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$9,195.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$3,575.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$8,470.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1997</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$8,540.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1998</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$8,090.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$9,620.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$9,980.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$6,145.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1997</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$9,285.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1998</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$1,820.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>actual</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$0.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>1999</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$2,340.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="19%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="20%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p>budget</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="28%" bgColor=#ffffff height=22>
<p align=right>$5,110.00</p>
</td>
</tr>
</tbody>
</table>
<p>Also, note that this was a single select query as opposed to the use of two crosstabs combined in a select query.</p>
<h3>Pivot Using IIF( ) and CHOOSE( ) Functions</h3>
<p>From the previous examples we see how to pivot a table on two columns. In all cases though, we were only able to show data for one accounting period, January. In order to show any of the other months activity using the above techniques we would have to create eleven more queries. Maintaining twelve queries, which only shows the data in one way is very cumbersome and over time as the number of reports needed increased, impossible to maintain.</p>
<p>The solution would be to write a single query with a parameter to be used in all twelve cases. The parameter would be the number of the month whose values we want to see. If we were to stay with just using the IIF( ) function our query would have fields like this:</p>
<p><dir>
<pre>[1997] = IIF([Year]="1999", (<dir>IIF([Month]=1, [JAN], (<dir>IIF([Month]=2, [FEB], …0) 0) 0)</dir></dir></pre>
<p></dir>
<p>This again would be impossible to maintain. </p>
<p>The alternative is to use the CHOOSE() function. The syntax is:</p>
<p><dir>
<pre> Choose (index, choice-1[, choice-2, ... [, choice-n]])</pre>
<p></dir>
<p>Where index refers to the choice number. If index equals 1 then choice-1 is returned, if it is 2 then choice-2 is returned, on down to index equal n then choice-n is returned.</p>
<p>Using our parameter we can rewrite the above function as:</p>
<p><dir>
<pre> [1997] = IIF([Year]="1997", Choose([Month], [JAN], [FEB], …,[DEC]), 0)</pre>
<div></div>
<p>with all twelve months listed in the single Choose() function. This is easier to understand and maintain. When the query is executed we are asked for a month and the correct values are returned. </p>
<p>The select query would look like the following:</p>
<p><dir>
<pre>PARAMETERS MonthNum Short;SELECT act.AcctID, act.DeptID, Sum(IIf([act].[Year]="1997",	Choose([MonthNum]		,[act].[Jan],[act].[Feb],[act].[Mar]		,[act].[Apr],[act].[May],[act].[Jun]		,[act].[Jul],[act].[Aug],[act].[Sep]		,[act].[Oct],[act].[Nov],[act].[Dec]),0)) AS 1997, Sum(IIf([act].[Year]="1998",	Choose([MonthNum]		,[act].[Jan],[act].[Feb],[act].[Mar]		,[act].[Apr],[act].[May],[act].[Jun]		,[act].[Jul],[act].[Aug],[act].[Sep]		,[act].[Oct],[act].[Nov],[act].[Dec]),0)) AS 1998, Sum(IIf([act].[Year]="1999" And [act].[Class]="actual",	Choose([MonthNum]		,[act].[Jan],[act].[Feb],[act].[Mar]		,[act].[Apr],[act].[May],[act].[Jun]		,[act].[Jul],[act].[Aug],[act].[Sep]		,[act].[Oct],[act].[Nov],[act].[Dec]),0)) AS Actual1999, Sum(IIf([act].[Year]="1999" And [act].[Class]="budget",	Choose([MonthNum]		,[act].[Jan],[act].[Feb],[act].[Mar]		,[act].[Apr],[act].[May],[act].[Jun]		,[act].[Jul],[act].[Aug],[act].[Sep]		,[act].[Oct],[act].[Nov],[act].[Dec]),0)) AS Budget1999, Sum(IIf([act].[Year]="2000" And [act].[Class]="budget",	Choose([MonthNum]		,[act].[Jan],[act].[Feb],[act].[Mar]		,[act].[Apr],[act].[May],[act].[Jun]		,[act].[Jul],[act].[Aug],[act].[Sep]		,[act].[Oct],[act].[Nov],[act].[Dec]),0)) AS Budget2000FROM Activity AS actGROUP BY act.AcctID, act.DeptID;</pre>
<p></dir>
<p>Note that we still need to use the sum() aggregate function for the query to return the correct results. When we execute the query we are asked for the parameter value for MonthNum. The results for a MonthNum of 1 (January) are:</p>
<table borderColor=#000000 cellSpacing=1 cellPadding=3 width=647 border=1>
<tbody>
<tr>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>AcctID</p>
</td>
<td vAlign=top width="10%" bgColor=#c0c0c0 height=22>
<p align=center>DeptID</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1997</p>
</td>
<td vAlign=top width="14%" bgColor=#c0c0c0 height=22>
<p align=center>1998</p>
</td>
<td vAlign=top width="16%" bgColor=#c0c0c0 height=22>
<p align=center>Actual1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget1999</p>
</td>
<td vAlign=top width="18%" bgColor=#c0c0c0 height=22>
<p align=center>Budget2000</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$1,955.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$3,925.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,195.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$3,575.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,470.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,540.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,090.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$9,620.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,980.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$6,145.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$9,285.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$1,820.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$2,340.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,110.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>2020</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,460.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$5,005.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$8,825.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,000.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$9,500.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>3000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$4,605.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,355.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$2,265.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$6,115.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,050.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>3010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$6,715.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$8,665.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$5,390.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$695.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,885.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>4000</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$540.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$9,085.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$7,775.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,550.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$8,255.00</p>
</td>
</tr>
<tr>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>1000</p>
</td>
<td vAlign=top width="10%" bgColor=#ffffff height=22>
<p>4010</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$7,885.00</p>
</td>
<td vAlign=top width="14%" bgColor=#ffffff height=22>
<p align=right>$1,035.00</p>
</td>
<td vAlign=top width="16%" bgColor=#ffffff height=22>
<p align=right>$2,870.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$1,265.00</p>
</td>
<td vAlign=top width="18%" bgColor=#ffffff height=22>
<p align=right>$5,510.00</p>
</td>
</tr>
</tbody>
</table>
<p>This result set matches the previous results as expected. If they had not then we would have known that this technique would not work. And if we need to see the values for any other month we would enter that month number into the parameter.</p>
<p></dir></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MS Access Pivots Part I</title>
		<link>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-i/</link>
		<comments>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-i/#comments</comments>
		<pubDate>Sat, 25 Sep 2004 21:35:59 +0000</pubDate>
		<dc:creator>darren</dc:creator>
				<category><![CDATA[SQL Origami]]></category>

		<guid isPermaLink="false">http://blog.ddpruitt.net/?p=3</guid>
		<description><![CDATA[
CrossTab Queries
CrossTab queries are an easy way in MS Access to pivot tables. They are limited in that you can only pivot on one field and can only aggregate one field.

As an example we look at Activity table in the Sample.mdb. If we were asked to show the January activity for each account &#8211; department [...]]]></description>
			<content:encoded><![CDATA[<div class=Section1>
<h2>CrossTab Queries</h2>
<p>CrossTab queries are an easy way in MS Access to pivot tables. They are limited in that you can only pivot on one field and can only aggregate one field.</p>
<p><span id="more-3"></span></p>
<p>As an example we look at Activity table in the Sample.mdb. If we were asked to show the January activity for each account &#8211; department combination for each year using a crosstab query, we would create the following SQL:</p>
<p>&nbsp;&nbsp;&nbsp;TRANSFORM Sum(Activity.Jan) AS SumOfJan<br />&nbsp;&nbsp;&nbsp;SELECT Activity.AcctID, Activity.DeptID<br />&nbsp;&nbsp;&nbsp;FROM Activity<br />&nbsp;&nbsp;&nbsp;WHERE (Activity.Class = &#8220;actual&#8221;)<br />&nbsp;&nbsp;&nbsp;GROUP BY Activity.AcctID, Activity.DeptID<br />&nbsp;&nbsp;&nbsp;PIVOT Activity.Year In (1997,1998,1999,2000);</p>
<p>The TRANSFORM identifies the column to aggregate and the type of aggregation (sum). Other types of aggregation possible are avg, min, max, count, etc.</p>
<p>The PIVOT identifies which column is pivoted on. In this case it is the Year column, the IN clause indicates that we only want to see these four specific column headings. If the IN clause was not specified then all possible values of Year would be used as column headings.</p>
<p>In the Query Builder, the Row Headings indicate which fields to show in the results, while the Column Heading shows the pivot field and the Value is the aggregated field. For a crosstab there is no limit to the number of Row Headings but you need one Column Heading and one Value. Of course with a crosstab you can have only one Column Heading and one Value.</p>
<p>The first eight rows of the result set would be:</p>
<table style="BACKGROUND: #fff" cellSpacing=1 cellPadding=1 border=1>
<tbody>
<tr style="BORDER-RIGHT: black solid; BORDER-TOP: black solid; BACKGROUND: silver; BORDER-LEFT: black solid; BORDER-BOTTOM: black solid">
<td>AcctID</td>
<td>DeptID</td>
<td>1997</td>
<td>1998</td>
<td>1999</td>
<td>2000</td>
</tr>
<tr>
<td>1000</td>
<td>1000</td>
<td>$1,955.00</td>
<td>$3,925.00</td>
<td>$9,195.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>2000</td>
<td>$8,540.00</td>
<td>$8,090.00</td>
<td>$9,620.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>2010</td>
<td>$9,285.00</td>
<td>$5,510.00</td>
<td>$1,820.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>2020</td>
<td>$8,460.00</td>
<td>$5,005.00</td>
<td>$8,825.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>3000</td>
<td>$4,605.00</td>
<td>$8,355.00</td>
<td>$2,265.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>3010</td>
<td>$6,715.00</td>
<td>$8,665.00</td>
<td>$5,390.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>4000</td>
<td>$540.00</td>
<td>$9,085.00</td>
<td>$7,775.00</td>
<td>$0.00</td>
</tr>
<tr>
<td>1000</td>
<td>4010</td>
<td>$7,885.00</td>
<td>$1,035.00</td>
<td>$2,870.00</td>
<td>$0.00</td>
</tr>
</tbody>
</table>
<p>As can be seen this is a very simple query, easy to setup and understand. Simply identify the column to pivot on and what column to aggregate and that is it. The problems start to arise whenever more than one pivot value or transform column is needed.</p>
<h2>Pivoting on Two Columns &#8211; Multiple Value Field Query</h2>
<p>Single CrossTab queries cannot pivot on more than one column. However, situations arise frequently where it is needed to pivot on two or more columns. One workaround for this, as documented in the Microsoft Knowledge Base (Q109939), is to build separate crosstab queries using the same underlying table, each pivoting on the same column but showing only specific values from the second column. The final result is obtained by joining the crosstab queries in a Select query. Microsoft refers to this as a <b>Multiple Value Field</b> query.</p>
<p>This method of joining crosstab queries is simple to implement whenever the number of columns to pivot on is small. However, as the number of column pivots increases the number of required joins also increases. Eventually a point is reached where the query is too complex to maintain or even to complex to execute. A rule of thumb should be that if it is needed to pivot on more than two columns then crosstab queries should not be used.</p>
<p>To create the Budget Variance and History report with a crosstab we will need to use a Multiple Value Field query. To accomplish this we use the previous crosstab query for our actual amounts and create the following crosstab for our budget amounts:</p>
<p>&nbsp;&nbsp;&nbsp;TRANSFORM Sum(Activity.Jan) AS SumOfJan<br />&nbsp;&nbsp;&nbsp;SELECT Activity.AcctID, Activity.DeptID<br />&nbsp;&nbsp;&nbsp;FROM Activity<br />&nbsp;&nbsp;&nbsp;WHERE (Activity.Class = &#8220;budget&#8221;)<br />&nbsp;&nbsp;&nbsp;GROUP BY Activity.AcctID, Activity.DeptID<br />&nbsp;&nbsp;&nbsp;PIVOT Activity.Year In (1997,1998,1999,2000);</p>
<p>As you can see the only difference is the WHERE clause.</p>
<p>Next we create the select query using both of the crosstab queries as the source. We join on the account and department numbers:</p>
<p>&nbsp;&nbsp;&nbsp;SELECT<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.AcctID, actual.DeptID,</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.[1997], actual.[1998],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.[1999], budget.[1999]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual.[2000], budget.[2000]<br />&nbsp;&nbsp;&nbsp;FROM<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[CrossTab Actual by Year] AS actual<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER JOIN [CrossTab Budget by Year] AS budget<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON (actual.DeptID = budget.DeptID)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND (actual.AcctID = budget.AcctID);
<p>In this case since we are currently in the year 1999 we would not have budget numbers for 1997 or 1998. The query builder looks like:</p>
<p>The results from this are:</p>
<table style="BACKGROUND: #fff" cellSpacing=1 cellPadding=1 border=1>
<tbody>
<tr style="BORDER-RIGHT: black solid; BORDER-TOP: black solid; BACKGROUND: silver; BORDER-LEFT: black solid; BORDER-BOTTOM: black solid">
<td>AcctID</td>
<td>DeptID</td>
<td>1997</td>
<td>1998</td>
<td>actual.1999</td>
<td>Budget.1999</td>
<td>actual.2000</td>
<td>budget.2000</td>
</tr>
<tr>
<td>1000</td>
<td>1000</td>
<td>$1,955.00</td>
<td>$3,925.00</td>
<td>$9,195.00</td>
<td>$3,575.00</td>
<td>$0.00</td>
<td>$8,470.00</td>
</tr>
<tr>
<td>1000</td>
<td>2000</td>
<td>$8,540.00</td>
<td>$8,090.00</td>
<td>$9,620.00</td>
<td>$9,980.00</td>
<td>$0.00</td>
<td>$6,145.00</td>
</tr>
<tr>
<td>1000</td>
<td>2010</td>
<td>$9,285.00</td>
<td>$5,510.00</td>
<td>$1,820.00</td>
<td>$2,340.00</td>
<td>$0.00</td>
<td>$5,110.00</td>
</tr>
<tr>
<td>1000</td>
<td>2020</td>
<td>$8,460.00</td>
<td>$5,005.00</td>
<td>$8,825.00</td>
<td>$9,000.00</td>
<td>$0.00</td>
<td>$9,500.00</td>
</tr>
<tr>
<td>1000</td>
<td>3000</td>
<td>$4,605.00</td>
<td>$8,355.00</td>
<td>$2,265.00</td>
<td>$6,115.00</td>
<td>$0.00</td>
<td>$5,050.00</td>
</tr>
<tr>
<td>1000</td>
<td>3010</td>
<td>$6,715.00</td>
<td>$8,665.00</td>
<td>$5,390.00</td>
<td>$695.00</td>
<td>$0.00</td>
<td>$5,885.00</td>
</tr>
<tr>
<td>1000</td>
<td>4000</td>
<td>$540.00</td>
<td>$9,085.00</td>
<td>$7,775.00</td>
<td>$8,550.00</td>
<td>$0.00</td>
<td>$8,255.00</td>
</tr>
<tr>
<td>1000</td>
<td>4010</td>
<td>$7,885.00</td>
<td>$1,035.00</td>
<td>$2,870.00</td>
<td>$1,265.00</td>
<td>$0.00</td>
<td>$5,510.00</td>
</tr>
</tbody>
</table>
<p>From this result set it is easy to directly compare the actual amounts to the budgeted amounts. Of course it must be remembered that these amounts are from just one month and as can be guessed trying to compare two separate months from the same year using a crosstab in this manner is cumbersome.</p>
<p>Using crosstabs for simple queries is easy. However for typical reporting needs crosstabs become too unwieldy and difficult to maintain. Fortunately we do have some alternatives.</p>
</p>
<p></></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.ddpruitt.net/2004/09/25/ms-access-pivots-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

