*!* sql生成的交叉表,如何转成vfp程序
*!* 在mysql论坛看到以下程序,
*!* 虽然我能用sele sum(iif(subject="语文",result,0) as 语文,。。。
*!* 做出他的效果,
*!* 但如果subject多的话就要写N条,太累了,
*!* 不如此程序来得简洁,
*!* 但不知如何能用vfp编出,
*!* 特来请高手赐教。
*!* 假设有张学生成绩表(CJ)如下
*!* Name Subject Result
*!* 张三 语文 80
*!* 张三 数学 90
*!* 张三 物理 85
*!* 李四 语文 85
*!* 李四 数学 92
*!* 李四 物理 82
*!* 想变成
*!* 姓名 语文 数学 物理
*!* 张三 80 90 85
*!* 李四 85 92 82
*!* declare @sql varchar(4000)
*!* set @sql = 'select Name'
*!* select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
*!* from (select distinct Subject from CJ) as a
*!* select @sql = @sql+' from test group by name'
*!* exec(@sql)
SET TALK OFF
SET SAFETY OFF
CLOSE ALL
IF FILE("CJ.DBF")
USE CJ.DBF
ELSE
CREATE TABLE CJ (姓名 C(8),科目 C(8),成绩 N(3))
INSERT INTO CJ (姓名,科目,成绩) VALUES ("张三","语文",80)
INSERT INTO CJ (姓名,科目,成绩) VALUES ("张三","数学",90)
INSERT INTO CJ (姓名,科目,成绩) VALUES ("张三","物理",85)
INSERT INTO CJ (姓名,科目,成绩) VALUES ("李四","语文",85)
INSERT INTO CJ (姓名,科目,成绩) VALUES ("李四","数学",92)
INSERT INTO CJ (姓名,科目,成绩) VALUES ("李四","物理",82)
ENDIF
cExpr="姓名 C(8)"
INDEX ON 科目 TO KM UNIQUE
GO TOP
DO WHILE !EOF()
cExpr=cExpr+","+ALLTRIM(科目)+" N(3)"
SKIP
ENDDO
SELECT 2
CREATE TABLE 成绩 ( &cExpr. )
SELECT 1
SET INDEX TO
GO TOP
DO WHILE !EOF()
cFileName=ALLTRIM(科目)
SELECT 2
LOCATE ALL FOR 姓名=A.姓名
IF EOF()
APPEND BLANK
REPLACE 姓名 WITH A.姓名
ENDIF
REPLACE &cFileName WITH A.成绩
SELECT 1
SKIP
ENDDO
SELECT 2
BROWSE
CLOSE ALL