>Dear Experts
>
>CREATE CURSOR testsub (name c(15),bill n(3),amount n(4))
>
>INSERT INTO testsub VALUES ('John',1,500)
>INSERT INTO testsub VALUES ('Eric',2,900)
>INSERT INTO testsub VALUES ('Eric',3,300)
>INSERT INTO testsub VALUES ('John',4,800)
>INSERT INTO testsub VALUES ('Eric',5,200)
>INSERT INTO testsub VALUES ('Bush',6,400)
>INSERT INTO testsub VALUES ('Eric',7,700)
>INSERT INTO testsub VALUES ('Bush',8,100)
>INSERT INTO testsub VALUES ('John',9,600)
>
>
>In a cursor, I need Sub-Total and Grand-Total as follows
>
>Bush----6----400
>Bush----8----100
>sub-T---2----500
>Eric----2----900
>Eric----3----300
>Eric----5----200
>Eric----7----700
>sub-T---4----2100
>John----1----500
>John----4----800
>John----9----600
>sub-T---3----1900
>Grd-T--19----4500
>
>Note: In the following line of sub total
>sub-T---2----500
>
>2 is number of bills and 500 is sum of both bills
>
>Please help
Hi Tariq
Add this after your code and try;
alter table alias() add column subtotal N(12,2)
alter table alias() add column subcnt N(5)
alter table alias() add column grandtotal N(12,2)
alter table alias() add column grandcnt N(5)
index on testsub.name+str(testsub.bill,3,0) tag tag1
local cGroup1
local nSubTTL,nSubCnt,nGrCnt,nGrTTL
store 0.00 to nGrCnt,nGrTTL
scan for .t.
store 0.00 to nSubTTL,nSubCnt
cGroup1= testsub.name
scan while testsub.name = cGroup1
nSubCnt=nSubCnt+1
nGrCnt=nGrCnt+1
nSubTTL=nSubTTL+testsub.amount
nGrTTL=nGrTTL+testsub.amount
endscan
skip -1
replace testsub.subtotal with nSubTTL
replace testsub.subcnt with nSubCnt
endscan
skip -1
replace testsub.grandtotal with nGrTTL
replace testsub.grandcnt with nGrCnt
browse normal
Well this is not something to advice,
(SQL statements are more educated way of doing it)
but defenetely something that is very good to know.
By extending this pattern of nested scan/endscans to number of levels
you need , you can do all sorts of grouped (pre)processing of cursors/tables
prior to executing complex reports. Old VFP reporting engine in esence is doing something very similar to this while executing your old frx-es. So If you need to preprocess your data for ANY grouped report prior to VFP9, you can do it relatively easy using this pattern.
If SQL statement cuts the mustard that is perfect, if not there is always another way :)