*--Sample only SET EXACT ON SET SAFETY OFF RELEASE tarray *--Create the table to store the product quantities *--This similates your select statement CREATE TABLE test (cproduct c(15), nqty N(3,0)) nval = 1 FOR i = 1 TO 10 m.cproduct = "PRODUCT_"+ALLTRIM(STR(nval)) m.nqty = nval nval = nval + 1 SELE test APPEND BLANK GATHER MEMVAR ENDFOR nval = 1 nqty = 2 FOR i = 1 TO 10 m.cproduct = "PRODUCT_"+ALLTRIM(STR(nval)) m.nqty = nqty nval = nval + 1 nqty = nqty + 1 SELE test APPEND BLANK GATHER MEMVAR ENDFOR *--Add the fields to store the totals for each product ALTER TABLE test ADD COLUMN ltotal L ALTER TABLE test ADD COLUMN ntotal N(10,0) ALTER TABLE test ADD COLUMN lheading L *--Store the totals to a temporary array DIMENSION tarray(1,2) GO TOP SCAN IF TYPE('tarray(1,1)')="L" tarray(1,1) = ALLTRIM(UPPER(test.cproduct)) tarray(1,2) = test.nqty ELSE lpos = ASCAN(tarray,ALLTRIM(UPPER(test.cproduct))) IF lpos = 0 DIMENSION tarray(ALEN(tarray,1)+1,2) tarray(ALEN(tarray,1),1) = ALLTRIM(UPPER(test.cproduct)) tarray(ALEN(tarray,1),2) = test.nqty ELSE tarray(lpos+1) = tarray(lpos+1)+test.nqty ENDIF ENDIF ENDSCAN *--Add the totals as records at the end of the table FOR i = 1 TO ALEN(tarray,1) SELE test APPEND BLANK IF i = 1 *--This is the first total, need to print heading TOTALS: REPLACE lheading WITH .T. ENDIF REPLACE ltotal WITH .T. REPLACE test.cproduct WITH tarray(i,1) REPLACE test.ntotal WITH tarray(i,2) ENDFOR GO top REPORT FORM test preview