Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / October 2006

Tip: Looking for answers? Try searching our database.

Comparing Two Tables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Xavier - 10 Oct 2006 17:08 GMT
Hello,

I was wondering if you guys would know the best way to pursue the
following problem.  I have two tables, one with the unique items and
another with all the changes that occurred to the item.  For example:

Table1
Model1 - Item1

Table2
Model1 - Item1 - RevA - Completion DateA
Model1 - Item1 - RevB - Completion DateB
Model1 - Item1 - RevC - Completion DateC

What I want to do is build a query that has Model#, Item# and the max
Completion Date.  The problem is that in some cases, an item could have
a revision that is still open.  In that case, I would want to see a
blank date next to the item#.  In other words, show the latest date of
completion for the Item# or a null value if there is still a revision
that is incomplete.  Does anyone have any recommendations as to how I
should pursue this problem?  Thanks!

Xavier
Ken Sheridan - 10 Oct 2006 18:22 GMT
Xavier:

Try this:

SELECT Model, Item,
MAX([Completion Date]) As [Latest Completion Date]
FROM Table2 AS T1
WHERE NOT EXISTS
  (SELECT *
   FROM Table2 AS T2
   WHERE T2.Model = T1.Model
        AND  T2.Item = T1.Item
         AND T2.[Completion Date] IS  NULL)
GROUP BY Model, Item
UNION ALL
SELECT Model, Item, [Completion Date]
FROM Table2
WHERE [Completion Date] IS NULL;

The first part of the UNION ALL operation returns those model/item rows
where there is no NULL completion date (identified by the subquery) grouping
by Model/Item and returning the latest date.  The second part of the
operation returns those roes where the completion date IS NULL.  There is no
need to group this part of the UNION ALL operation as I assume there will no
more than one open revision per model/item.  If there can be more than one
simply change the UNION ALL to a UNION operation as the latter suppresses
duplicates.  A UNION operation is slower than a UNION ALL, however, so should
only be used when necessary.

Ken Sheridan
Stafford, England

> Hello,
>
[quoted text clipped - 19 lines]
>
> Xavier
John Spencer - 10 Oct 2006 19:00 GMT
One way should be

SELECT Model, Item, Rev, CompletionDate
FROM Table2
WHERE Nz(CompletionDate,#1/1/9999#) =
   (SELECT Max(Nz(CompletionDate,#1/1/9999#))
    FROM Table2 as Temp
    WHERE Temp.Model = Table2.Model)

> Hello,
>
[quoted text clipped - 19 lines]
>
> Xavier
Xavier - 11 Oct 2006 15:49 GMT
Thanks guys, got both methods to work!

Xavier
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.