Solved

Output max datetime instance in multi repsonse query

Posted on 2014-12-29
3
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

705 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