Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Why does this get me multiple records? Expand / Collapse
Author
Message
Posted Wednesday, February 13, 2013 11:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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
Post #1419660
Posted Wednesday, February 13, 2013 11:45 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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

Post #1419673
Posted Wednesday, February 13, 2013 12:24 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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.
Post #1419692
Posted Wednesday, February 13, 2013 12:30 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!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?)

Post #1419698
Posted Wednesday, February 13, 2013 12:33 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!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

Post #1419703
Posted Wednesday, February 13, 2013 12:52 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:46 AM
Points: 21,832, Visits: 27,858
Sean Grebey (2/13/2013)
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


What are you trying to return? Without having DDL (CREATE TABLE statement), sample data (INSERT INTO statements), expected results based on sample data; all we can give you are shots in the dark.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1419712
Posted Wednesday, February 13, 2013 12:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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.
Post #1419714
Posted Wednesday, February 13, 2013 12:57 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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.
Post #1419717
Posted Wednesday, February 13, 2013 1:00 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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.
Post #1419718
Posted Wednesday, February 13, 2013 1:23 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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.
Post #1419728
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse