2011-01-14 66 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;