回复 10楼 schtg
同一分类,0分虽不影响各学科的总分数,但0分的不计人数,各学科的总人数可能不是相同的,那就要分类分科统计总人数了。能不能优化一下数据结构,分输入数据结构(原始数据结构)、输出数据结构(显示数据结构)
tm=SECONDS() USE cj DIMENSION afs[FCOUNT()-5] && yw及之后的字段名列表 FOR i=6 TO FCOUNT() afs[i-5] = FIELD(i) ENDFOR * 创建平均分表 cmd = "CREATE CURSOR pjf (dsdm n(6),xqdm n(8),xxdm n(10)" FOR i=1 TO ALEN(afs) cmd = cmd + "," + afs[i]+"zrs I," + afs[i]+"pjf n(12,2)," + afs[i]+"lkpx I," + afs[i]+"bspx I," + afs[i]+"bxpx I" ENDFOR cmd = cmd + ")" EXECSCRIPT(cmd) ? "从 cj 获取 xxdm 的人数和分数" SELECT DISTINCT dsdm,xqdm,xxdm FROM cj ORDER BY dsdm,xqdm,xxdm INTO CURSOR tmp SELECT * FROM pjf WHERE .t. INTO CURSOR pjf_xxdm READWRITE APPEND FROM DBF("tmp") INDEX on xxdm TAG tag_xxdm SELECT cj SET RELATION TO xxdm INTO "pjf_xxdm" tj_rsfs("pjf_xxdm") SET RELATION TO ? "从 pjf_xxdm 获取 xqdm 的人数和分数" SELECT DISTINCT dsdm,xqdm FROM pjf_xxdm ORDER BY dsdm,xqdm INTO CURSOR tmp SELECT * FROM pjf WHERE .t. INTO CURSOR pjf_xqdm READWRITE APPEND FROM DBF("tmp") INDEX on xqdm TAG tag_xqdm SELECT pjf_xxdm SET RELATION TO xqdm INTO "pjf_xqdm" tj_rsfs("pjf_xqdm") SET RELATION TO ? "从 pjf_xqdm 获取 dsdm 的人数和分数" SELECT DISTINCT dsdm FROM pjf_xqdm ORDER BY dsdm INTO CURSOR tmp SELECT * FROM pjf WHERE .t. INTO CURSOR pjf_dsdm READWRITE APPEND FROM DBF("tmp") INDEX on dsdm TAG tag_dsdm SELECT pjf_xqdm SET RELATION TO dsdm INTO "pjf_dsdm" tj_rsfs("pjf_dsdm") SET RELATION TO ? "从 pjf_dsdm 获取总人数和总分数" INSERT INTO pjf (dsdm) VALUES (0) SELECT pjf_dsdm tj_rsfs("pjf") ? "统计 各类平均分数" tj_pjf("pjf") tj_pjf("pjf_dsdm") tj_pjf("pjf_xqdm") tj_pjf("pjf_xxdm") ? "统计 联考名次、本市名次、本县名次" FOR i=1 TO ALEN(afs) tj_mc(afs[i], "pjf_xqdm") tj_mc(afs[i], "pjf_xxdm") ENDFOR * 整合各类表 SELECT pjf APPEND FROM DBF("pjf_dsdm") APPEND FROM DBF("pjf_xqdm") APPEND FROM DBF("pjf_xxdm") ? SECONDS()-tm SELECT * FROM pjf CLOSE TABLES ALL RETURN * 统计人数分数 FUNCTION tj_rsfs(cAlias) SCAN FOR i=1 TO ALEN(afs) rs = EVALUATE(IIF(cAlias=="pjf_xxdm", "1", afs[i]+"zrs")) fs = EVALUATE(IIF(cAlias=="pjf_xxdm", afs[i], afs[i]+"pjf")) IF fs != 0 REPLACE (cAlias+"."+afs[i]+"zrs") WITH EVALUATE(cAlias+"."+afs[i]+"zrs") + rs,; (cAlias+"."+afs[i]+"pjf") WITH EVALUATE(cAlias+"."+afs[i]+"pjf") + fs ENDIF ENDFOR ENDSCAN ENDFUNC * 统计平均分 FUNCTION tj_pjf(cAlias) SELECT (cAlias) SCAN FOR i=1 TO ALEN(afs) rs = EVALUATE(afs[i]+"zrs") fs = EVALUATE(afs[i]+"pjf") IF rs != 0 REPLACE (afs[i]+"pjf") WITH fs/rs ENDIF ENDFOR ENDSCAN ENDFUNC * 统计 联考名次、本市名次、本县名次 FUNCTION tj_mc(kmm, cAlias) eKey = kmm+"pjf" SELECT (cAlias) INDEX on &eKey TAG kmm DESCENDING tj_lkpx(kmm) &&联考名次 INDEX on dsdm*1000+&eKey TAG kmm DESCENDING tj_bspx(kmm) &&本市名次 IF cAlias == "pjf_xxdm" INDEX on xqdm*1000+&eKey TAG kmm DESCENDING tj_bxpx(kmm) &&本县名次 ENDIF ENDFUNC * 联考名次 FUNCTION tj_lkpx(kmm) m = 1 n = 1 fs = -1 SCAN IF EVALUATE(kmm+"pjf") != fs fs = EVALUATE(kmm+"pjf") m = n ENDIF n = n + 1 REPLACE (kmm+"lkpx") WITH m ENDSCAN ENDFUNC * 本市名次 FUNCTION tj_bspx(kmm) m = 1 n = 1 fs = -1 dm = 0 SCAN IF dsdm != dm dm = dsdm m = 1 n = 1 ENDIF IF EVALUATE(kmm+"pjf") != fs fs = EVALUATE(kmm+"pjf") m = n ENDIF n = n + 1 REPLACE (kmm+"bspx") WITH m ENDSCAN ENDFUNC * 本县名次 FUNCTION tj_bxpx(kmm) m = 1 n = 1 fs = -1 dm = 0 SCAN IF xqdm != dm dm = xqdm m = 1 n = 1 ENDIF IF EVALUATE(kmm+"pjf") != fs fs = EVALUATE(kmm+"pjf") m = n ENDIF n = n + 1 REPLACE (kmm+"bxpx") WITH m ENDSCAN ENDFUNC