Solved

Output max datetime instance in multi repsonse query

Posted on 2014-12-29
3
97 Views
Last Modified: 2014-12-29
I have a query to retrieve the last input repsonse to a query.  Below is a sample of the output for two accounts where there are different responses for each account.  I want to only output the last response based on the datetime stamp field.




MaxOfDateTime      VisitID      QueryID      AccountNumber      Response
12/18/2014 2:02:00 PM      A10000418661      CM.LACETOT      A00000260884      3
12/18/2014 3:23:00 PM      A10000418661      CM.LACETOT      A00000260884      4
12/24/2014 7:50:00 AM      A10000419402      CM.LACETOT      A00000261452      1
12/26/2014 1:16:00 PM      A10000419402      CM.LACETOT      A00000261452      2

Here is the SQL for the query above:

SELECT Max(dbo_NurQueryResults.DateTime) AS MaxOfDateTime, dbo_NurQueryResults.VisitID, dbo_NurQueryResults.QueryID, dbo_BarVisits.AccountNumber, dbo_NurQueryResults.Response
FROM dbo_NurQueryResults LEFT JOIN dbo_BarVisits ON dbo_NurQueryResults.VisitID = dbo_BarVisits.VisitID
GROUP BY dbo_NurQueryResults.VisitID, dbo_NurQueryResults.QueryID, dbo_BarVisits.AccountNumber, dbo_NurQueryResults.Response
HAVING (((dbo_NurQueryResults.VisitID) In ("A10000418661","A10000419402")) AND ((dbo_NurQueryResults.QueryID)="CM.LACETOT"))
ORDER BY dbo_BarVisits.AccountNumber;

Open in new window


How do I modify the query to only show the one line for each account?

Thanks

Glen
0
Comment
Question by:GPSPOW
3 Comments
 
LVL 39

Expert Comment

by:als315
ID: 40522298
Can you explain field Response? If you remove it, you will be able group other results. If you need this field, you will need nested queries: in first - remove Response and group data, in second - join these results with your query by all four fields
0
 
LVL 8

Accepted Solution

by:
fabriciofonseca earned 500 total points
ID: 40522339
You will need to create 2 queries.

In the first one you will group by "VisitID" and max "DateTime". Call it query1 for example.

Then you will create a query2 where you use both fields of query1 as key in the join.

If you send your DB it will be easier to send you the full SQL, but you may reach the solution  with the tip above.
0
 

Author Closing Comment

by:GPSPOW
ID: 40522514
Thanks that worked.

Glen
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now