Solved

Output max datetime instance in multi repsonse query

Posted on 2014-12-29
3
96 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 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

21 Experts available now in Live!

Get 1:1 Help Now