我有数据库表P,看起来像这样:
date wellID rate
01/01/1988 well01 300
01/02/1988 well01 301
01/03/1988 well01 310
........
01/04/1988 well02 500
01/05/1988 well02 490
01/06/1988 well02 495
........
01/04/1988 well03 700
01/05/1988 well03 750
01/06/1988 well03 700
........
01/07/1989 well04 400
01/08/1989 well04 350
01/09/1989 well04 340
........
我想提取每口井的初始速率,即每口井最早记录的日期以及相关的速率。我期待这样的事情:
date wellID oil
01/01/1988 well01 300
01/04/1988 well02 500
01/04/1988 well03 700
01/07/1989 well04 400
我正在尝试使用以下SQL查询从Access中提取此数据
SELECT p.date, p.wellID, p.oil
FROM p
LEFT JOIN
(
SELECT wellID, min(date) AS MinDate
FROM p
) AS a
ON p.wellID = a.wellID
WHERE p.date = a.MinDate;
但Access提供此消息“您试图执行不包含指定表达式'WellId'作为聚合函数的一部分的查询”。我担心这超出了我对SQL的有限知识,所以所有人都非常感谢
谢谢!
答案 0 :(得分:2)
添加group by
,您可能希望此案例INNER JOIN
INNER JOIN
(
SELECT wellID, min(date) AS MinDate
FROM p
GROUP BY wellID
) AS a
答案 1 :(得分:0)
您在内部聚合查询中遗漏了一个group by子句。
试试这个:
SELECT p.date, p.wellID, p.oil
FROM p
LEFT JOIN
(
SELECT wellID, min(date) AS MinDate
FROM p
GROUP BY wellID
) AS a
ON p.wellID = a.wellID
WHERE p.date = a.MinDate;