?
Solved

MySQL query in Access

Posted on 2014-04-11
4
Medium Priority
?
363 Views
Last Modified: 2014-04-14
A collegue requested I modify this query (for MS Access) so that he gets the latest date (adate) from the projectactivity table as well as the list of projects:

original query:
SELECT * FROM projects 
WHERE projects.active='yes'

Open in new window


I gave him this query, which works well in MySQL:
SELECT * FROM projects 
LEFT OUTER JOIN (SELECT projectactivity.projectid as id,max(adate) as latestdate FROM projectactivity WHERE projectactivity.projectid='10335-2') as derivedTable ON projects.ProjectID=id
WHERE projects.active='yes' 

Open in new window


I don't use Access enough to know why this does not work for him in Access.  

NOTE:In his MS Access setup, he is connecting to the same MySQL tables that I tested my query on.  He uses Access to format the MySQL data into printable report formats.
0
Comment
Question by:Zipbang
  • 2
4 Comments
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 39993904
Try this

SELECT * FROM projects 
    LEFT OUTER JOIN (
       SELECT projectactivity.projectid as id,max(adate) as latestdate 
       FROM projectactivity WHERE projectactivity.projectid='10335-2'
     ) AS derivedTable 
     ON projects.ProjectID=derivedTable.id
WHERE projects.active='yes' 

Open in new window

0
 

Author Comment

by:Zipbang
ID: 39994366
Julian,

This helps, but I think I misunderstood his request.

He wants a query that shows  all active projects (active='yes') from the projects table and the latest adate (max(adate)) for each one when adate is in the table named projectactivity

SELECT * FROM projects 
--  join on the latest date from the projectactivity table, where projectactivity.id=projects.id
WHERE projects.active='yes'
 

Open in new window


Sorry for the confusion, am I clear here?
0
 
LVL 41

Accepted Solution

by:
Sharath earned 1000 total points
ID: 39995067
remove the WHERE clause and I think you missed the GROUP BY clause.
SELECT * FROM projects 
    LEFT OUTER JOIN (
       SELECT projectactivity.projectid as id,max(adate) as latestdate 
       FROM projectactivity GROUP BY projectactivity.projectid
     ) AS derivedTable 
     ON projects.ProjectID=derivedTable.id
WHERE projects.active='yes' 

Open in new window

0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39995911
Just checking, why do you have this filter in your sub query

WHERE projectactivity.projectid='10335-2'

Will you only ever by searching on that projectid?
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question