*************************************************************************** * Description.......: GetMedianValue - returns median value from a specified table * Calling Samples...: GetMedianValue('curPrice','LstSlPrice') * Parameter List....: pcTableName, pcField, pcWhere * Created by........: Daniel Rouleau #025397 - original idea * Modified by.......: Nadya Nosonovsky 10/25/2000 01:30:02 PM ***************************************************************************** * Returns median value or .f. for unsuccessful cases lparameters pcTableName, pcField, pcWhere * pcTableName - name of the table or already opened cursor, which should be processed * pcFiled - name of the field, which used in calculation, price, for example * pcWhere - where expression suitable for macro * Both parameters are optional, if they are not specified, current working alias is used and price field ** Check parameter first if empty(pcTableName) or vartype(pcTableName)<>'C' pcTableName=alias() && Current open alias if empty(pcTableName) && No current table return .f. endif endif if empty(pcField) or vartype(pcField)<>'C' pcField=upper('price') else pcField=upper(pcField) endif if empty(pcWhere) or vartype(pcWhere)<>'C' pcWhere='' endif local lnMiddleRecord, lcCursor, lnOldSelect, lnMedianValue lnOldSelect=select() && Save current area lcCursor='cur'+sys(2015) && Unique name if used(lcCursor) && Should never happen use in (lcCursor) endif select &pcField from (pcTableName) ; where &pcWhere ; into cursor (lcCursor) order by 1 descending if _tally>3 lnMiddleRecord=floor(_tally/2) else =messagebox('Number of records is less than 3. Can not calculate median...',48) return .f. endif if used(lcCursor) go top in (lcCursor) skip lnMiddleRecord in (lcCursor) lnMedianValue = evaluate(lcCursor + '.' + pcField) use in (lcCursor) endif select (lnOldSelect) && Return to the original area return lnMedianValue>Nadya: