I would combine the records with SQL - SELECT, with a GROUP BY clause.
The running total can best be handled with report variables - if it is for a report. For a SELECT - SQL statement, you might fetch all records which are less than
gomonth(2005, 1), for example. Perhaps an UPDATE statement would be appropriate for this. I am not sure whether a subquery would do the trick in this case.
As to the requirement of including all months, you can create a table that has one record for each month, and combine your other query, the one that has the totals, with the list of months, with a LEFT JOIN.
>Let's a say that I have a simple table that has columns WIDGETNAME, SALEDATE. The task is to create a table with columns WIDGETNAME, SALESPERMONTH, RUNNINGTOTAL.
A requirement is that months with zero sales are to be included in the final tally. So for a given widget, the final table might look like this:
>Note: SPM=sales per month
>Item Month SPM RunningTot
>THISWIDGET 2005/01 002 002
>THISWIDGET 2005/02 001 003
>THISWIDGET 2005/03 010 013
>THISWIDGET 2005/04 000 013
>THISWIDGET 2005/05 002 015
>
>This is not particularly onerous to do with some VFP code (and an application of INDEX UNIQUE that I like to use) layered on top of some SQL. But I was wondering if anybody has an SQL-only solution for this? It seems this would be a commonly requested type of summary. Thanks very much.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)