Here's a generic transpose function. It will transform the first 254 rows of any table into columns and use column 1 for the original header names. Just change the "MyTable" to your desired target table.
FOR lnFieldCounter = 1 TO AFIELDS(laFields)
lnMaxWidth=MAX(lnMaxWidth,laFields(lnFieldCounter,3))
ENDFOR
FOR lnRowCounter = 1 TO MIN(RECCOUNT(),254)
lcBuildTable=lcBuildTable+IIF(lnRowCounter=1,"",",")+" COL"+TRANSFORM(lnRowCounter)+" C("+TRANSFORM(lnMaxWidth)+")"
ENDFOR
CREATE TABLE tbl_transformed (Headers c(10),&lcBuildTable)
FOR lnRecords= 1 TO MAX(1,lnFieldCounter-1)
APPEND BLANK
ENDFOR
SELECT (lcMyTable)
lcColsToTrans=AFIELDS(laFields)
SCAN FOR RECNO()<=254
lnTransCol=RECNO()
FOR lnCounter = 1 TO lcColsToTrans
lcVar="Var"+TRANSFORM(lnCounter)
&lcVar=EVALUATE(laFields(lnCounter,1))
ENDFOR
SELECT tbl_transformed
FOR lnCounter2 = 1 TO lcColsToTrans
GO lnCounter2
lcVar="Var"+TRANSFORM(lnCounter2)
REPLACE (FIELD(lnTransCol+1)) WITH TRANSFORM(&lcVar)
ENDFOR
ENDSCAN
SELECT tbl_transformed
FOR lnCounter = 1 TO lcColsToTrans
GO lnCounter
REPLACE (FIELD(1)) WITH (laFields(lnCounter,1))
ENDFOR
GO TOP
SET TALK &lctalk
ENDPROC
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.