|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:47 AM
Points: 30,
Visits: 55
|
|
Trying to return just a single record for the max audit date for an agent. I thought this would do it:
select completed, Max(auditDate) from tblAudit_AuditSchedule where agentNumber = '54321' group by auditdate, completed HAVING auditDate = MAX(auditDate)
But I get:
0 2011-12-24 00:00:00 0 2012-11-07 00:00:00 0 2013-02-04 07:30:00 0 2013-02-13 00:00:00 0 2013-02-15 00:00:00 0 2013-02-20 00:00:00 1 2013-02-22 00:00:00
Not sure why the HAVING clause doesn't limit it to just the last record. Any help on what I am doing wrong would be appreciated.
Sean
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 329,
Visits: 1,227
|
|
Hi Sean
The MAX is applied to each group and since the auditdate column is in the GROUP BY clause you will get a group for each auditdate. This means that your HAVING clause is effectively just doing auditdate = auditdate and you will get a returned row for each.
If you remove the auditdate from you GROUP BY clause you will get up to 2 returned rows in your query, since you also have the completed column in your GROUP BY clause and this appears to be a bit flag.
I would say that you don't need to have a GROUP BY clause, but you will need to determine how you want the completed column handled, eg MAX or some other aggregate function.
I suspect that you are looking for the last audit record for the agent, so you could try something like
SELECT TOP 1 completed, auditdate FROM tblAudit_AuditSchedule WHERE agentNumber = '54321' ORDER BY auditdate DESC
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:47 AM
Points: 30,
Visits: 55
|
|
| SQL told me I couldn't have auditdate in the HAVING CLAUSE without it being in the GROUP BY CLAUSE which is the only reason I added it. And the MAX(auditdate) can't be in the WHERE because it is an aggregate, so I'm confused about how to do it.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 7:21 AM
Points: 983,
Visits: 13,356
|
|
You need to remove the auditdate from the grouping and in your HAVING clause you need max(auditdate) = xxxx (depends on what you want it to equal?)
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 7:21 AM
Points: 983,
Visits: 13,356
|
|
infact looking again at your post you dont even need a HAVING clause. you just need the max
select Max(auditDate) from tblAudit_AuditSchedule where agentNumber = '54321' if you need it per agent
select AgentNumber, Max(auditDate) from tblAudit_AuditSchedule group by AgentNumber and if you need it per agent and competed status
select completed, AgentNumber, Max(auditDate) from tblAudit_AuditSchedule group by Completed, AgentNumber
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 9:46 AM
Points: 21,832,
Visits: 27,858
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:47 AM
Points: 30,
Visits: 55
|
|
Let me explain it a little bit better. Every agentNumber will have dozens of audits, so for example:
agentNumber auditdate completed 54321 1/1/2013 1 54321 1/15/2013 1 54321 2/1/2013 0 12345 1/1/2013 1 12345 1/8/2013 1 12345 1/17/2013 1
I need to know the value of completed for each agentNumber on their Max(auditdate), i.e.
54321 0 12345 1
SO really all I need to get back is an agentNumber matched with if their last audit was completed. I don't even care what the last audit date is, just if it was completed or not.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:47 AM
Points: 30,
Visits: 55
|
|
| And thanks for the help by the way. Much appreciated.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 9:47 AM
Points: 30,
Visits: 55
|
|
Animal Magic (2/13/2013)
infact looking again at your post you dont even need a HAVING clause. you just need the max select Max(auditDate) from tblAudit_AuditSchedule where agentNumber = '54321' if you need it per agent select AgentNumber, Max(auditDate) from tblAudit_AuditSchedule group by AgentNumber and if you need it per agent and competed status select completed, AgentNumber, Max(auditDate) from tblAudit_AuditSchedule group by Completed, AgentNumber
Kind of the other way around, I need the completed value by agent only for their last auditdate. Thanks for the input though.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 329,
Visits: 1,227
|
|
There are a number of ways of doing this. Here's a few ways of writing essentially the same query.
SELECT completed, auditdate, a.agentNumber FROM tblAudit_AuditSchedule a INNER JOIN ( SELECT agentNumber, MAX(auditdate) maxdate FROM tblAudit_AuditSchedule GROUP BY agentNumber ) b ON a.agentNumber = b.agentNumber and a.auditdate = b.maxdate
-- or SELECT completed, auditdate, agentNumber FROM tblAudit_AuditSchedule a WHERE auditdate = ( SELECT MAX(auditdate) FROM tblAudit_AuditSchedule b WHERE a.agentNumber = b.agentNumber )
-- or ;with LastAuditDatePerAgent AS ( SELECT agentNumber, MAX(auditdate) maxdate FROM tblAudit_AuditSchedule GROUP BY agentNumber ) SELECT completed, auditdate, a.agentNumber FROM tblAudit_AuditSchedule a INNER JOIN LastAuditDatePerAgent b ON a.agentNumber = b.agentNumber and a.auditdate = b.maxdate The previous query I put in would have worked for a single agentNumber.
|
|
|
|