Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create spreadsheet from normalized data
Message
From
09/11/2001 09:31:52
 
 
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00579569
Message ID:
00579622
Views:
21
Einar,
If you know in advance which codes are in the table, you could something like this:
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 test
This 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
&lcSelect
HTH
>Hi,
>
>Can you use sql to transform a dbf table with fields like "code" and "Value" so I get each "code" as a column?
>
>Transform records like
>A1 123
>A2 234
>A3 345
>
>to
>
> A1 A2 A3
>123 234 345
Daniel
Previous
Reply
Map
View

Click here to load this message in the networking platform