SELECT SUM(IIF(code="A1", value, 000000000.00)) AS A1,; SUM(IIF(code="A2", value, 000000000.00)) AS A2,; SUM(IIF(code="A3", value, 000000000.00)) AS A3; FROM testThis is quick and dirty and has the risk that you might ignore some codes. Better would be to build the SQL SELECT on the fly based on the existing codes. This will only work if you have less than 255 values (max. number of fields in VFP). Something like this:
*-- create some test data CREATE CURSOR test (code C(2), value N(10)) INSERT INTO test VALUES("A1", 1) INSERT INTO test VALUES("A2", 2) INSERT INTO test VALUES("A3", 3) INSERT INTO test VALUES("A4", 4) INSERT INTO test VALUES("A5", 5) INSERT INTO test VALUES("A6", 6) INSERT INTO test VALUES("A7", 7) INSERT INTO test VALUES("A8", 8) INSERT INTO test VALUES("A9", 9) *-- get the existing codes into a cursor SELECT DISTINCT code FROM test INTO CURSOR Codes IF _TALLY > 254 MESSAGEBOX("Too many fields") RETURN ENDIF lcFieldList = "" *-- loop through codes and build SQL field list SCAN lcFieldList = lcFieldList + "SUM(IIF(code='" + code + "', value, 000000000.00)) AS " + code + "," ENDSCAN *-- build SQL SELECT lcSelect = "SELECT " + LEFT(lcFieldList, LEN(lcFieldList)-1) + " FROM test " *-- run SQL SELECT &lcSelectHTH