views:

95

answers:

2

Hi All

I know there are similar postings out there, but when I have tried to use any of them in my own query I can not get it working.

Basically, I have 3 tables that I want to query in ACCESS using the SQL view.

Initially though and for this example I am trying to do it with just the 2.

tb1 name: Tasks

tb1 fields wanted: Task ID Task Title Project ID (This is to be used to grab the project title at the end, i.e. the 3rd table as mentioned above)

tb2 name: Task Notes

tb2 fields wanted: Task Note ID Task Note Date Task Note

What was happening using the design view, is that if there was 3 notes for example relating to one task in the Tasks table, it was listing all 3 task notes.

What I want to do, is to bring out the last Task Note entered only, be it by [Task Note ID] or by [Task Note Date].

What I am left with now, built from help from here is the following SQL statement:

SELECT 

t.[Task ID], 
t.[Task Title], 
t.[Project ID],
tn.*

FROM
Tasks t

INNER JOIN [Task Notes] tn ON t.[Task ID] = tn.[Task ID]

WHERE tn.[Task ID] = 
  (SELECT max(tn.[Task Note Date]) FROM tn where tn[Task ID] = t.[Task ID])
;

I am getting a syntax error with this, but previous incarnations have also left me stuck.

I normally develop outside of ACCESS and in php/asp it is fine, however in ACCESS I am a newbie.

Any help would be greatly appreciated

Cheers

Matthew

+1  A: 

Try something like this

SELECT t.[Task ID], 
       t.[Task Title], 
       t.[Project ID],
       tn.*    
  FROM Tasks t 
       INNER JOIN
       (
          SELECT [Task ID], 
                 MAX([Task Note Date]) MaxDate
            FROM [Task Notes] 
           GROUP 
              BY [Task ID]
       ) TaskNoteIDS 
          ON t.[Task ID] = TaskNoteIDS.[Task ID] 
       INNER JOIN [Task Notes] tn 
          ON TaskNoteIDS.[Task ID] = tn.[Task ID]
             AND TaskNoteIDS.MaxDate = [Task Notes].[Task Note Date];
astander
HiWhat is TaskNoteIDS in the above example?Is it a typo, or is it a temp table. I am getting syntax errors with this, and then when i try to use [Task Notes] instead it still gives me syntax errors (which are really unhelpful MS)CheersMatthew
TaskNoteIDS is a sub select. try thisSELECT *FROM (Tasks AS t INNER JOIN (SELECT [Task Notes].[Task ID], Max([Task Notes].[Task Note Date]) AS [MaxDate]FROM [Task Notes]GROUP BY [Task Notes].[Task ID]) AS TaskNoteIDS ON t.[Task ID] = TaskNoteIDS.[Task ID]) INNER JOIN [Task Notes]ON (TaskNoteIDS.MaxDate = [Task Notes].[Task Note Date]) AND ([Task Notes].[Task ID] = TaskNoteIDS.[Task ID]);
astander
A: 

Try:

FROM tasks AS t
FROM [other table] AS tn

It's an "SQL flavor variant" found in MS Access ...

Smandoli
Won't work as posted - you need the trailing period after the closing square bracket. And if you have badly-named fields/tables that require brackets, they can't be referenced inside the derived table. Also, if you're in SQL 92 mode, you can use standard ().
David-W-Fenton