这是一个完整的例子。我是在 Oracle 中完成的,因此您可能需要稍微调整一下 Access 的语法。
CREATE TABLE tbl1 (NAME VARCHAR2(100), score INT, Dt DATE);
INSERT INTO tbl1 VALUES ('John',10,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('John',20,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('John',5,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('Harry',15,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('Frank',20,to_date('20121101','YYYYMMDD'));
select a.dt
, b.name
, a.score
FROM (SELECT dt
, MAX(score) score
FROM tbl1
GROUP BY dt) a
, (SELECT NAME
, dt
, MAX(score) score
FROM tbl1
GROUP BY name, dt) b
WHERE a.dt = b.dt
AND a.score = b.score
输出:
1 11/1/2012 Frank 20
2 9/1/2012 John 20
请注意,如果两个人在给定的一天得分最高,它将打印他们两个。例如:
INSERT INTO tbl1 VALUES ('John',10,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('John',20,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('John',5,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('Harry',20,to_date('20120901','YYYYMMDD'));
INSERT INTO tbl1 VALUES ('Frank',20,to_date('20121101','YYYYMMDD'));
select a.dt
, b.name
, a.score
FROM (SELECT dt
, MAX(score) score
FROM tbl1
GROUP BY dt) a
, (SELECT NAME
, dt
, MAX(score) score
FROM tbl1
GROUP BY name, dt) b
WHERE a.dt = b.dt
AND a.score = b.score
输出:
DT NAME SCORE
1 11/1/2012 Frank 20
2 9/1/2012 John 20
3 9/1/2012 Harry 20