Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting last 10 records 1

Status
Not open for further replies.

StarhawkGamer

Programmer
Jul 7, 2008
17
US
I have two tables, 1 has a list of agents, the other is quality control surveys for all agents. is there a way in Access to get the last 10 QAs for each agent in a single query?
 
Code:
SELECT a.agentname
     , s.surveyname
     , s.surveydate
  FROM agents AS a
INNER
  JOIN surveys AS s
    ON s.agentID = a.ID
   AND ( SELECT COUNT(*) 
           FROM surveys  
          WHERE agentID = s.agentID
            AND surveydate > s.surveydate ) < 10

r937.com | rudy.ca
 
How would you change it if you were just doing it with the single table and getting the agent's ID to cross-reference in Excel?
 
I guess I'm just not understanding the second select statement in the query. Could you explain the logic behind the line
Code:
AND ( SELECT COUNT(*) 
           FROM surveys  
          WHERE agentID = s.agentID
            AND surveydate > s.surveydate ) < 10
 
okay, here's what that subquery does

it counts the number of surveys for the same agent that have a later date than this survey

if that count is less than 10, then this survey has to be among the latest 10 for this agent

which is what you wanted, right?

"get the last 10 QAs for each agent in a single query"

r937.com | rudy.ca
 
Ok, attempted to set it up and I keep getting a syntax error on the subquery.

Code:
SELECT Agents.[Advocate ID], QA.[SessionID], QA.[FATAL IR]
FROM Agents
INNER JOIN QA
ON QA.[UNIT ID] = Agents.[Advocate ID]
AND ( SELECT COUNT(*)
FROM QA
WHERE [UNIT ID] = QA.[UNIT ID]
AND [Survey Date] QA.[Survey Date]) < 10;
 
um, yeah, i think that's msaccess, being petulant again

the ON clause may need parentheses

oh, and you must use an alias for QA in the outer query, the way i did
Code:
  FROM Agents
INNER 
  JOIN QA [green]AS foo[/green]
    ON [red]([/red]
       [green]foo[/green].[UNIT ID] = Agents.[Advocate ID]
   AND ( SELECT COUNT(*)
           FROM QA
          WHERE [UNIT ID] = [green]foo[/green].[UNIT ID]
            AND [Survey Date] > QA.[Survey Date] ) < 10
       [red])[/red]

r937.com | rudy.ca
 
tried those changes and still get a syntax error. here is the altered code.

Code:
SELECT Agents.[Advocate ID], foo.[SessionID], foo.[FATAL IR]
FROM Agents
INNER JOIN QA as foo
ON (
foo.[UNIT ID] = Agents.[Advocate ID]
AND ( SELECT COUNT(*)
FROM QA
WHERE [UNIT ID] = foo.[UNIT ID]
AND [SURVEY DATE] > qa.[SURVEY DATE]) < 10);

I'm using MSAccess 2007, if that makes any difference.
 
crikey!! let's see what happens now...
Code:
SELECT Agents.[Advocate ID]
     , foo.[SessionID]
     , foo.[FATAL IR]
  FROM Agents
     , QA as foo
 WHERE foo.[UNIT ID] = Agents.[Advocate ID]
   AND ( SELECT COUNT(*)
           FROM QA
          WHERE [UNIT ID] = foo.[UNIT ID]
            AND [SURVEY DATE] > foo.[SURVEY DATE]) < 10

r937.com | rudy.ca
 
Thank you. It works perfectly!!! Made one change from doing it by date to doing it by survey ID# (because multiple surveys can be done on a single day)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top