You might take a look at the WITH statement: <a href="http://www.postgresql.org/docs/8.4/static/queries-with.html" target="_blank">http://www.postgresql.org/docs/8.4/static/queries-with.html</a>. <div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div><br></div><div>
I would try using WITH RECURSIVE to create a table of three columns: original_category, current_category, parent_category. For example, the WITH RECURSIVE would create this table:</div>
<div>Transportation | Fuel | NULL</div><div>Transportation | Auto | Fuel</div><div>Transportation | Transportation | Auto</div>
<div><br></div><div>The SELECT statement JOINS the WITH results on <b>original_category</b>. Something like this maybe...</div><div>SELECT </div><div>    cats.current_category,</div>
<div>    SUM(tbltransdetails.amount)</div><div>FROM </div><div>    tbltransdetails </div><div>    INNER JOIN cats ON </div><div>        cats.original_category = tbltransdetails.category_id</div><div>WHERE cats.parent_category IS NULL</div>


<div>GROUP BY cats.current_category</div><div><br></div></div></div></blockquote></div></blockquote><div><br>Hi Robert,<br><br>I appreciate your help.  I believe you have the right idea, but being a novice still, I do not really understand how to apply this approach.  I tried following this example (which is what I think you may have been thinking of):<br>
WITH RECURSIVE cat_iwant(original_category, current_category, parent_category) AS<br>  (SELECT original_category, current_category, parent_category FROM tblcategory <br>  WHERE current_category = 'category_name'<br>
UNION ALL<br>SELECT c.original_category, c.current_category, c.parent_category<br>    FROM cat_iwant cw, tblcategory c<br>    WHERE c.category_name = cw.original_category)<br>SELECT <br>    cats.current_category,<br>    SUM(tbltransdetails.amount)<br>
FROM <br>    tbltransdetails <br>    INNER JOIN cats ON <br>        cats.original_category = tbltransdetails.category_id<br>WHERE cats.parent_category IS NULL<br>GROUP BY cats.current_category;<br><br>No, I don't quite see how to apply it to my specific situation.  I assumed (for better or worse) that your suggested 3 columns are simply column names to be used for the new (temporary, I assume?) table, but I get an error that 'original_category' does not exist.  I did try using my actual column names from tblcategory, but that did not help any either.  Not that I thought it would, but I didn't know what else to try.<br>
<br>If it helps, I can send you a spreadsheet with some sample data to give you a better idea of the tables/data involved.  Also, don't hesitate to criticize my category table design - I just borrowed from a web tutorial, and thought I could apply it to my db.  If there is a better way to skin the cat, I'm all ears.  That said, I think your suggestion is probably a good one.  And I really should learn to use this type of query - might come in handy in other situations.  :-)<br>
<br>I'm also curious as to the performance of this type of query - when I tried the initial example with the integer, it seemed fairly rapid.  I also tried it using the example below the one I pasted in here (from the docs link you posted) - that produced a 270-row table that I did not think would be usable for the rest of the query.  But it seems efficient enough for general purposes.<br>
<br>Thanks again for taking a look at my problem.<br>Blessings,<br>Don<br></div></div>