2011-01-14 67 views
0

因此,我有一个包含不同元素和日期的表。 它基本上是这样的:获取访问查询中每个元素的最小日期

actieElement beginDatum 
1    1/01/2010 
1    1/01/2010 
1    10/01/2010 
2    1/02/2010 
2    3/02/2010 

我现在需要的是每个actieElement最小的日期。 我发现了一个使用简单的GROUP BY语句的解决方案,但是这样查询就失去了它的范围,你不能再改变任何东西。

没有GROUP BY语句,我得到每个actieElement的多个日期,因为某些日期是相同的。

我想到了这样的事情,但它也不起作用,因为它会给子查询更多的则1个记录:

SELECT s1.actieElement, s1.begindatum 
FROM tblActieElementLink AS s1 
WHERE (((s1.actieElement)=(SELECT TOP 1 (s2.actieElement) 
      FROM tblActieElementLink s2 
      WHERE s1.actieElement = s2.actieElement 
      ORDER BY s2.begindatum ASC))); 
+0

你是什么意思“OU不能改变什么了”,准确地什么是你想要做 – Mark 2011-01-14 14:43:15

+0

MS Access不允许更新查询的连接(显式或隐式的),这里一面该连接不可更新。在这些情况下我们使用临时表;没有别的办法。 – Arvo 2011-01-14 14:54:16

回答

1

试试这个

SELECT s1.actieElement, s1.begindatum 
FROM tblActieElementLink AS s1 
WHERE s1.begindatum =(SELECT MIN(s2.begindatum) 
      FROM tblActieElementLink s2 
      WHERE s1.actieElement = s2.actieElement 
     ); 
-1

添加DISTINCT子句您选择。

0
SELECT DISTINCT T1.actieElement, T1.beginDatum 
    FROM tblActieElementLink AS T1 
     INNER JOIN (
        SELECT T2.actieElement, 
          MIN(T2.beginDatum) AS smallest_beginDatum 
        FROM tblActieElementLink AS T2 
        GROUP 
         BY T2.actieElement 
       ) AS DT1 
      ON T1.actieElement = DT1.actieElement 
      AND T1.beginDatum = DT1.smallest_beginDatum;