Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

Output max datetime instance in multi repsonse query

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
GPSPOW
Asked:
GPSPOW
1 Solution
 
als315Commented:
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
 
fabriciofonsecaCommented:
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
 
GPSPOWAuthor Commented:
Thanks that worked.

Glen
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now