Please do not make assumptions on what VFP 7 or any other version cannot do while ms access can. In fact, even after 10+ years since VFP development has been stopped, VFP is still too much ahead of ms access (which I don't even classify as a database system - if you would compare their sql capabilities know that VFP is closer to ANSI SQL while ms access has an sql understanding of its own that makes you crazy).
The SQL you tried wouldn't make sense in any SQL database system, including ms access, ms sql server, postgresql, mysql, oracle ... you name it.
It has been a long time I last used VFP 7, I am not sure if it supported subqueries in that sense if it would work after being corrected. However, if you are doing this from inside a VFP7 application (or VFP 7 IDE), you could simply use multiple SQLs instead of subqueries to get the end result you want. i.e.:
Select bank, Sum(amount) As amount ;
from adv_1412 ;
where s_no<>9999 And bank In (1114,1123) ;
group By a.bank ;
into cursor crsBankA ;
nofilter
Select bank, Sum(amount) As amount ;
from adv_1512 ;
where s_no<>9999 And bank In (1114,1123) ;
group By bank ;
into cursor crsBankB ;
nofilter
Select Nvl(a.bank, b.bank) As bank, a.amount As amountA, b.amount As AmountB ;
from crsBankA a ;
full join crsBankB b on a.bank = b.bank
OTOH, be it a VFP5, 6, 7 application or a non-VFP application, it could use the VFPOLEDB and thus utilise the VFP 9's data engine with subqueries and much more ANSI SQL support. Then say you could do this:
Select Nvl(a.bank, b.bank) As bank, a.amount As amountA, b.amount As AmountB ;
from ;
(Select bank, Sum(amount) As amount ;
from adv_1412 ;
where s_no<>9999 And bank In (1114,1123) ;
group By a.bank) a ;
full Join ;
(Select bank, Sum(amount) As amount ;
from adv_1512 ;
where s_no<>9999 And bank In (1114,1123) ;
group By bank) b On a.bank = b.bank
Having said these, you are saying time period, but in your code there is nothing that deals with periods (those codes and different table names suggest you have a problem in data design but if that works for you then might be acceptable).
You are also saying
export to excel and make pivot tables
Well, even in VFP6 or 7 ... 9, for creating pivot tables in excel:
- You don't need to sum(marize) data. That is what Excel excels at < g > while creating pivots and summarizing data.
- You would pass the data to excel using VFPOLEDB (and thus using VFP9 data engine, not 7).
Here is a sample pivoting using VFP's sample data (I don't remember if I wrote this during VFP7 or 6 days but I know I have been using this infrastructure much before than VFP9 and 8):
*!* Excel Pivot sample
*!* Author:Cetin Basoz
*!* Thank to Marco Plaza for reminding me about saving Recordset as a workaround for 64 bits
Local loADOStream, lcSQL, lcConnStr
TEXT TO m.lcSQL NOSHOW TEXTMERGE PRETEXT 15
SELECT RTRIM(emp.first_name) + ' ' +RTRIM(emp.last_name) as SalesMan,
cs.company, pr.prod_name as ProductName, oi.quantity, YEAR(od.order_date) as yearOrdered
FROM customer cs
INNER JOIN orders od on cs.cust_id = od.cust_id
inner join employee emp on od.emp_id = emp.emp_id
INNER JOIN orditems oi on od.order_id = oi.order_id
INNER JOIN products pr on oi.product_id = pr.product_id
ENDTEXT
lcConnStr = 'Provider=VFPOLEDB;Data Source='+_samples+'Data\Testdata.dbc'
loADOStream = GetDataAsAdoStream( m.lcConnStr, m.lcSQL)
* Pivot sample
** Assume 64 bit excel and use ADO stream
*#include 'xlConstants.h'
#Define xlCount -4112
#Define xlSum -4157
#Define xlDataField 4
#Define xlExternal 2
lcPageList = 'Company'
lcRowList = 'ProductName,YearOrdered'
lcColList = 'SalesMan'
lcDataField = 'Quantity'
Alines(laRowFields,m.lcRowList,.T.,",")
Alines(laColFields,m.lcColList,.T.,",")
Alines(laPageFields,m.lcPageList,.T.,",")
lcOption = 'sum'
lcFunction = Iif(Lower(Evl(m.lcOption,'')) == "count","COUNT","SUM")
lcCaption = Iif(Upper(m.lcFunction)='SUM','Quantity Sold','Count')
lnFunction = Iif(Upper(m.lcFunction)='SUM',xlSum,xlCount)
Local oExcel As 'Excel.Application'
oExcel = Createobject('Excel.Application')
With oExcel
.Visible = .T.
.Workbooks.Add
*-- Destination of the pivottable inside Excel
.ActiveWorkbook.ActiveSheet.Name = 'Product Sales'
With .ActiveWorkbook.ActiveSheet
oDestination = .Range('A1')
With oExcel.ActiveWorkbook.PivotCaches.Add(xlExternal)
loRS = Createobject('AdoDb.Recordset')
loRS.Open( loADOStream )
.Recordset = loRS
.CreatePivotTable(oDestination, 'PivotTable')
Endwith
With .PivotTables("PivotTable")
Do Case
Case !Empty(m.lcRowList) And !Empty(m.lcColList) And !Empty(m.lcPageList)
.AddFields(@laRowFields, @laColFields, @laPageFields)
Case !Empty(m.lcRowList) And !Empty(m.lcColList)
.AddFields(@laRowFields, @laColFields)
Case !Empty(m.lcRowList) And !Empty(m.lcPageList)
.AddFields(@laRowFields, , @laPageFields)
Case !Empty(m.lcRowList)
.AddFields(@laRowFields)
Case !Empty(m.lcColList) And !Empty(m.lcPageList)
.AddFields(, @laColFields, @laPageFields)
Case !Empty(m.lcColList)
.AddFields(, @laColFields)
Case !Empty(m.lcPageList)
.AddFields(, , @laPageFields)
Endcase
.PivotFields(m.lcDataField).Orientation = xlDataField
With .Datafields(1)
.Caption = m.lcCaption
.Function = m.lnFunction
Endwith
If !Empty(m.lcRowList)
For ix = 1 To Alen(laRowFields)
With .PivotFields(laRowFields[ix])
.Subtotals(1) = .T. && To turn of subtotals
.Subtotals(1) = .F.
Endwith
Endfor
.Mergelabels = .T.
Endif
Endwith
.UsedRange.Columns.AutoFit
Endwith
If Type('.ActiveWorkbook.ShowPivotTableFieldList') = 'L'
.ActiveWorkbook.ShowPivotTableFieldList = .F.
Endif
Endwith
Procedure GetDataAsAdoStream(tcConnection, tcSQL)
loStream = Createobject('AdoDb.Stream')
loConn = Createobject("Adodb.connection")
loConn.ConnectionString = m.tcConnection
loConn.Open()
loRS = loConn.Execute(m.tcSQL)
loRS.Save( loStream )
loRS.Close
loConn.Close
Return loStream
Endproc
.