*: Pivot.prg *: J Chris Powell, 2000 *: Please report any bugs to jchris@kscable.com *: Pass the alias of the table you are wanting to summarize *: The field that you want summarized *: The criteria that will define the columns in the new cursor *: And the criteria that will define the rows (Optional, if only one row is desired) *: Example: * PIVOT("Sales", "Amount", "SalesPerson") * Creates a cursor with a column for every salesperson with the total sales * PIVOT("Sales", "Amount", "Month", "SalesPerson") * Creates a cursor with a row for every salesperson and a column for every month. *: Returns the number of records generated LPARAMETER Alias, field, column, row LOCAL SQL, FieldToGet SQL = '' FieldToGet = m.Alias + '.' + m.Column SELECT &FieldToGet. as Column ; FROM (Alias) ; GROUP BY &Column. ; INTO CURSOR HoldPivot IF _Tally > 0 SCAN SQL = m.SQL + IIF(EMPTY(m.SQL),'',',') + "SUM(IIF(" + m.Column + ' = "' + ALLTRIM(HoldPivot.Column) + '", ' + m.Field + ', 0)) AS ' + ALLTRIM(HoldPivot.Column) ENDSCAN SQL = 'SELECT ' + IIF(EMPTY(m.Row),'',m.Row + ', ') + m.SQL SQL = m.SQL + ' FROM ' + m.Alias IF NOT EMPTY(m.row) SQL = m.SQL + ' GROUP BY ' + m.row ENDIF &SQL. ENDIF USE IN HoldPivot RETURN _TallyHope that helps