vf表行列互转的问题 (转贴)
程序代码:
CREATE CURSOR T1 (XM C(10),KM C(10),FS I) INSERT INTO T1 VALUES ('张三','语文',60) INSERT INTO T1 VALUES ('李二','语文',61) INSERT INTO T1 VALUES ('汪一','数学',58) INSERT INTO T1 VALUES ('马六','地理',91) INSERT INTO T1 VALUES ('马六','数学',60) INSERT INTO T1 VALUES ('李二','地理',30) INSERT INTO T1 VALUES ('张三','数学',20) INSERT INTO T1 VALUES ('张三','地理',39) *-- 以上生成测试数据,这里假设你的原表名为 T1 *--- 行记录转换为列记录 SELECT KM INTO CURSOR TMP FROM T1 GROUP BY KM SELECT TMP LCSTR="SELECT XM" SCAN LCSTR=LCSTR+",SUM(IIF(ALLTRIM(KM)=='"+ALLTRIM(KM)+"',FS,0)) AS "+ALLTRIM(KM)+; IIF(RECNO()<RECCOUNT(),[],[ FROM T1 GROUP BY XM INTO CURSOR T2]) ENDSCAN MESSAGEBOX(LCSTR) &LCSTR BROWSE *--- 列记录转换为行记录 SELECT T2 LCSTR=[] FOR I=2 TO FCOUNT() LCSTR=LCSTR+'SELECT XM,FIELD('+TRANSFORM(I)+') AS KM,EVALUATE(FIELD('+TRANSFORM(I)+')) AS FS FROM T2 WHERE NOT EMPTY(EVALUATE(FIELD('+TRANSFORM(I)+')))'+; IIF(I>2,')',[])+IIF(I<FCOUNT(),' UNION (',' INTO CURSOR T3') ENDFOR =MESSAGEBOX(LCSTR) &LCSTR BROWSE