Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Table from Vertical to horizontal
Message
From
08/12/2008 06:50:38
 
 
To
06/12/2008 23:43:24
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01365804
Message ID:
01365924
Views:
12
Will it be possible to convert a vertical table to horizental?

Here is the code that I use to transform a cursor with multiple rows for each month and year into a cursor where the different years represent the columns and each row represents a moth. Hopefully, you can adapt this working and tested code to suit your own needs:
Local lcFieldName, lnLen, liFld, laYears[ 1 ], lcVarName, lcCurKey, liYear, lnAmt, liNdx, lcYear

*** For this method to work, it assumes that the cursor coming in has the
*** month identifier in the first column, the number we want to aggregate
*** is in the second column and the year indentifier is in the third column.
*** get the field from the results cursor that we are grouping by
lcFieldName = Field( 1, 'csrResults' )
lnLen = Len( Evaluate( 'csrResults.' + lcFieldName ) )

*** Save csrResults
Select * From csrResults Into Cursor csrSave NOFILTER
Use In csrResults

Create Cursor csrResults ( &lcFieldName C( lnLen ) )

*** Find out how many distinct years there are
Select Distinct nYear From csrSave Order By nYear Into Array laYears

*** Now add the columns for whatever years we have
*** Store the field names in some variables
For liFld = 1 To Alen( laYears, 1 )
  lcVarName = 'Year' + Transform( laYears[ liFld ] )
  Alter Table csrResults Add Column &lcVarName N( 14, 2 )
Endfor
*** Now scan through the original cursor
*** and make the results "horizontal" for all years
Select csrSave
lcCurKey = ""
Scan
  If Not( Alltrim( Evaluate( 'csrSave.' + lcFieldName ) ) == lcCurKey )
    *** Time to insert a new record
    lcCurKey = Alltrim( Evaluate( 'csrSave.' + lcFieldName ) )
    Insert Into csrResults( &lcFieldName ) Values ( lcCurKey )
  Endif
  lcYear = 'Year' + Transform( Evaluate( Field( 3, 'csrSave' ) ) )
  lnAmt = Evaluate( Field( 2, 'csrSave' ) )
  Replace ( lcYear ) With lnAmt In csrResults
Endscan
Previous
Reply
Map
View

Click here to load this message in the networking platform