程序代码:
CLEAR ALL
CLEAR
SET SAFETY OFF
USE in_data IN 0
my_Count("in_data", "單位", "貨物類別", "out_data")
USE IN in_data
RETURN
*-----------------------------------------
* 目標:對輸入表進行計數統計,將結果輸出到自由表中
* 參數:tcInTable -- 數據來源的輸入表別名
* tcRowField -- 統計單位,是輸入表的一個字段名,將作爲輸出表的行名稱
* tcColField -- 統計對象,是輸入表的一個字段名,將作爲輸出表的列名稱
* tcOutTable -- 輸出的自由表名,新生成,將覆蓋磁盤上已有的同名文件
* 機制:將統計數據生成在内存二維數組中,然後將此數組的内容輸出到指定表。
* 用法:見本程序示例
*-----------------------------------------
PROCEDURE my_Count(tcInTable AS Character, tcRowField AS Character, tcColField AS Character, tcOutTable AS Character)
LOCAL lcField AS Character, lnRecord AS Integer
LOCAL laTable[1,1]
LOCAL lnRow AS Integer, lnCol AS Integer
LOCAL lcString AS Character
* 獲取行標題和列標題
lcField = tcInTable + "." + tcRowField
SELECT DISTINCT &lcField AS R1 FROM (tcInTable) INTO CURSOR cursor_Rows
lcField = tcInTable + "." + tcColField
SELECT DISTINCT &lcField AS C1 FROM (tcInTable) INTO CURSOR cursor_Cols
* 將統計結果儲存到二維數組中
DIMENSION laTable[RECCOUNT("cursor_Rows"), RECCOUNT("cursor_Cols")]
STORE 0 TO laTable
lnRecord = RECNO(tcInTable)
GOTO TOP IN (tcInTable)
DO WHILE !EOF(tcInTable)
SELECT cursor_Rows
LOCATE FOR UPPER(ALLTRIM(cursor_Rows.R1)) == UPPER(ALLTRIM(EVALUATE(tcInTable + "." + tcRowField)))
lnRow = RECNO("cursor_Rows")
SELECT cursor_Cols
LOCATE FOR UPPER(ALLTRIM(cursor_Cols.C1)) == UPPER(ALLTRIM(EVALUATE(tcInTable + "." + tcColField)))
lnCol = RECNO("cursor_Cols")
laTable[lnRow, lnCol] = laTable[lnRow, lnCol] + 1
SKIP IN (tcInTable)
ENDDO
GOTO lnRecord IN (tcInTable)
* 創建輸出表結構,lcString 為 CREATE TABLE 命令字段結構格式字符串
lcString = "( " + tcRowField + " C( " + ALLTRIM(STR(LEN(EVALUATE(tcInTable + "." + tcRowField)))) + "), "
SELECT cursor_Cols
SCAN ALL
lcString = lcString + ALLTRIM(cursor_Cols.C1) + " I, "
ENDSCAN
lcString = LEFT(lcString, LEN(lcString) - 2) + " )"
* 創建輸出表
IF USED(tcOutTable)
USE IN (tcOutTable)
ENDIF
CREATE TABLE (tcOutTable) FREE &lcString
FOR lnRow = 1 TO ALEN(laTable,1)
GOTO lnRow IN cursor_Rows
SELECT (tcOutTable)
APPEND BLANK
REPLACE (FIELD(1, tcOutTable)) WITH cursor_Rows.R1
FOR lnCol = 1 TO ALEN(laTable,2)
REPLACE (FIELD(lnCol + 1, tcOutTable)) WITH laTable[lnRow, lnCol]
NEXT
NEXT
* 關閉本過程打開的臨時文件
USE IN (tcOutTable)
USE IN cursor_Rows
USE IN cursor_Cols
ENDPROC
輸入表in_data.dbf的内容:
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
輸出表out_data.dbf的結果:
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[
本帖最后由 TonyDeng 于 2011-7-29 02:15 编辑 ]