*!* 测试数据
SET CENTURY ON
SET DATE YMD
CREATE CURSOR tt(序号 i,日期 d,物料 c(20),价格 n(10,2))
INSERT INTO tt(序号,日期,物料,价格) VALUES (1,{^2018-05-01},"电脑01",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (2,{^2018-05-02},"电脑01",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (3,{^2018-05-03},"电脑01",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (4,{^2018-05-01},"电脑02",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (5,{^2018-05-04},"电脑02",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (6,{^2018-05-06},"电脑02",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (7,{^2018-05-02},"电脑03",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (8,{^2018-05-05},"电脑03",INT(RAND()*10))
INSERT INTO tt(序号,日期,物料,价格) VALUES (9,{^2018-05-06},"电脑03",INT(RAND()*10))
* BROWSE
LOCAL tmp
tmp = GetMax("TT","日期","物料")
IF !EMPTY(tmp)
SELECT (tmp)
BROWSE
ELSE
MESSAGEBOX("参数有问题")
ENDIF
*!* 调用函数,参数正常时,返回非空值的临时表名称
FUNCTION GetMax(cTableName,cDateField,cGroupField)
LOCAL tmp_TableName,cSql
tmp_TableName = ""
cSql = ""
*!* 检测源表
IF TYPE("cTableName") = "C"
AND USED(cTableName) AND TYPE("cDateField") = "C" AND TYPE("cGroupField") = "C"
tmp_TableName = LOWER("tmp"+SYS(2015))
TEXT TO cSql TEXTMERGE NOSHOW PRETEXT 4
SELECT t1.* FROM <<cTableName>> AS t1 ;
RIGHT JOIN (SELECT MAX(<<cDateField>>) AS <<cDateField>> ,<<cGroupField>> FROM <<cTableName>> GROUP BY <<cGroupField>>) AS t2 ;
ON t1.<<cGroupField>> = t2.<<cGroupField>> and t1.<<cDateField>> = t2.<<cDateField>> ;
INTO CURSOR <<tmp_TableName>> READWRITE ;
ORDER BY t1.<<cGroupField>>
ENDTEXT
*
messagebox(cSql) &&观察生成的SQL语句对不对
EXECSCRIPT(cSql)
ENDIF
RETURN tmp_TableName
ENDFUNC
[此贴子已经被作者于2018-5-5 14:04编辑过]