0

Can any one please suggest me whats the wrong with my query.

SELECT SUM(cCallDuration), COUNT(*), AVG(cCallduration), 
       cBeginTime, cEndTime, cAnswerTime, cCallDuration, cDispatcherName, cConsoleName,
       cEndpointName, cProfileName, cCallDirection, cCallType,
       cCallNature, cCallData, cDirectedCall
FROM  CALLINFO 
WHERE cBeginTime >='7/11/2011 12:00:00 AM' 
     AND cEndTime <='7/11/2011 12:00:00 AM' 
     AND cCallType='InBound' 
GROUP BY cConsoleName

I'm getting this error:

In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = ,cBeginTime ]

2
  • 2
    '7/11/2011 12:00:00 AM' is not a safe format for a datetime literal - depending on settings, SQL Server may interpret that date as 7th November or 11th July. '2011-07-11T12:00:00' would be safely, unambiguously, 11th July. Also, if you want time to default to midnight, you can omit the time portion - use '20110711' in that case. Commented Jul 5, 2011 at 6:38
  • @Damien - thx, I never knew that. Do you have a reference at hand backing that up? Commented Jul 5, 2011 at 6:42

2 Answers 2

2

You have to have more in your GROUP BY clause

Check out this example. Every column that is not aggregate is included in the GROUP BY. You must do that also

SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate

Try this one:

SELECT SUM(cCallDuration), COUNT(*), AVG(cCallduration), 
       cDispatcherName, cConsoleName,              
FROM  CALLINFO 
WHERE cBeginTime >='7/11/2011 12:00:00 AM' 
     AND cEndTime <='7/11/2011 12:00:00 AM' 
     AND cCallType='InBound' GROUP BY cConsoleName,cDispatcherName

so to complete your full sql syntax you have to include every column that is not an aggregate in your GROUP BY clause and that means every column except the SUM,AVG and COUNT

Sign up to request clarification or add additional context in comments.

Comments

1

GROUP BY clause : Columns in any nonaggregate expression in the SELECT list must be included in the GROUP BY list.

SELECT SUM(cCallDuration), COUNT(*), AVG(cCallduration), 
       cBeginTime, cEndTime, cAnswerTime, cCallDuration, cDispatcherName, cConsoleName,
       cEndpointName, cProfileName, cCallDirection, cCallType,
       cCallNature, cCallData, cDirectedCall
FROM  CALLINFO 
WHERE cBeginTime >='7/11/2011 12:00:00 AM' 
     AND cEndTime <='7/11/2011 12:00:00 AM' 
     AND cCallType='InBound' GROUP BY 
        cBeginTime, cEndTime, cAnswerTime, cCallDuration, cDispatcherName, cConsoleName,
       cEndpointName, cProfileName, cCallDirection, cCallType,
       cCallNature, cCallData, cDirectedCall

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.