473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Taking corresponding record using max function

I have a table with 3 columns. The columns are "Case Number", "Agent ID", and "Time the Agent Touched Case". There are multiple records of the same "Case Number" since a case can be touched by multiple agents at different times. I am trying to find the last agent who touched the case and the time they touched the case. I can find the time by using the Max function in a query, but how do I get Access to return the corresponding agent? Thanks in advance.
Sep 7 '10 #1
8 2707
NeoPa
32,556 Expert Mod 16PB
I'd use a subquery to identify the max time then link this data in to the original table to find only the relevant records. Be careful though. If cases can have multiple agents touching them at the same time, then you may need to handle this scenario specially or get multiple results.

Anyway, something like this should work for you :
Expand|Select|Wrap|Line Numbers
  1. SELECT   tT.[Case Number]
  2.        , tT.[Agent ID]
  3.        , tT.[Time the Agent Touched Case]
  4. FROM     [Table] AS tT
  5.          INNER JOIN
  6.     (
  7.     SELECT   [Case Number]
  8.            , Max([Time the Agent Touched Case]) AS MaxTime
  9.     FROM     [Table] AS tTi
  10.     GROUP BY [Case Number]
  11.     ) AS sT
  12.   ON     tT.[Case Number] = sT.[Case Number])
  13.  AND     tT.[Time the Agent Touched Case] = sT.[MaxTime]
Sep 7 '10 #2
Still haven't found a process for this yet. Right now, I am grouping first by "Case Number" and then doing a Max for "Time the Agent Touched Case." If I include "Agent ID" as a group by after "Time the Agent Touched Case", I'll get multiple records with the same case number. I am trying to only find the "Agent ID" associated with the last "Time the Agent Touched Case".
Sep 13 '10 #3
NeoPa
32,556 Expert Mod 16PB
You don't say why the suggested idea wouldn't work.

Have you tried it like that?

If so what happened?
Sep 13 '10 #4
liimra
119 100+
Another simple approach is to use the Last function
Expand|Select|Wrap|Line Numbers
  1. DLast("[Agent ID]","[TableName]","[Case Number] = " & [NumberFieldOnTHeForm])
One thing you might want to do is to sort the table or query (recommended) according to time so you make sure you always getting the right correspondent.

Regards,
Ali
Sep 13 '10 #5
NeoPa
32,556 Expert Mod 16PB
I'm afraid that wouldn't work Ali (except maybe coincidentally sometimes).
Sep 14 '10 #6
liimra
119 100+
Thanks NeoPa, you are right. I though it would work because I once implemented it in payroll database (salaries in advance --> last payment) and I can't recall I got any problems there. Maybe, because users were inputing data in order and so DLast was getting the actual last record. Anyways, for this particular problem we can still overcome it using the DLast by specifying two criteria instead of one (Case Number and DMax of Date); so it becomes
Expand|Select|Wrap|Line Numbers
  1. =DLast("[AgentID]","QueryName","CaseNumber =   [CaseNumber]  
  2. And [DateFieldName] = #" & DMax("DateFieldName","QueryName","CaseNumber =" & [CaseNumber]) & "#")
I tried it and it works without any problems and even if many agents touch(edit) the case on the same date, still this expression will get the last one as time value is stored as fraction of 24 hours.

Thanks again & Regards,
Ali
Sep 14 '10 #7
NeoPa
32,556 Expert Mod 16PB
It can be made to work Ali (You would need to take more care with Date literals of course - See Literal DateTimes and Their Delimiters (#)), but is there a good reason to go to all the trouble, when the Max is what is really being searched for? Don't let the name confuse you into thinking you're looking for the last item. You're looking for the item with the Max date.

It's almost an accident that it would be referred to as last in normal language. Read normal language as saying this is last {when sorted in date order}, where the phrase within {} is the default and needn't be stated.
Sep 15 '10 #8
liimra
119 100+
True. I totally agree, the main thing here is the MAX; Lookup should give the same outcome.

Regards,
Ali
Sep 15 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: jagadeeshbp | last post by:
Hi, I am having a CPP file which defines a class: class xyz{ public: int (*funcptr)(void); } A function foo is defined in a "C file"
18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
1
by: Mike | last post by:
In C, we can typedef pointer to functions, and therefore use function tables. But what's the advantage of using function table? Thanks, Mike
2
by: rajvbprogramer | last post by:
here is my problem. i have two records. one containing 20 records and second containing 30 records. now i want the recordcount property to use and go to the 21 record. i m calculating the...
1
by: Invicta | last post by:
There are two fields I need to copy preferably using a command button. If possible I would also like to stipulate how many new records I want. I've made a start using the in built Duplicate Record...
5
by: mukeshrasm | last post by:
Hi I am displaying record using php. I am giving the record structure in html format. Admin will allow user to view the no. of record user will view. <table>...
17
by: ganesh raj | last post by:
Hi, want to write an plsql code to update a single record using rowid where all the rows are duplicated could someone help me in this case. Prod_service_id Location feature ord...
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
1
by: mbbum88 | last post by:
Hi there, I'm still relatively new to Access and am trying to build a database to track applicants for jobs. The form I'm having a problem with is one for "Editing applications". I want to use...
2
by: raamay | last post by:
hello experts, i have fetched records from mysql database and against each record i want to place a update tag which when clicked will update the corresponding record using ajax and php. But here...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.