Solved

finding max time in calculations

Posted on 2014-01-14
5
245 Views
Last Modified: 2014-04-18
I need to find the max time difference between two times.
I am getting an aggregate error.
SELECT CLE_IDX_CHAN, COUNT(CLE_IDX_OFFHOOK) ''Call Count'', SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)) ''Seconds'',
            CONVERT(VARCHAR(12),DATEADD(ss, AVG(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) ''Avg Length'',
            CONVERT(VARCHAR(12),DATEADD(ss, SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) ''Total Length'',
            MAX(CONVERT(VARCHAR(12),DATEADD(ss, SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) )
            FROM CL_EVENTS_IDX WITH (NOLOCK)
                  INNER JOIN CL_EVENTS A WITH (NOLOCK) ON CLE_IDX_KEY = CLE_ID
                  INNER JOIN UTILITY_INFO WITH (NOLOCK) ON CLE_IDX_UCODE = UTILITY_KEY
                  INNER JOIN CL_EVENTS B WITH (NOLOCK) ON A.CLE_ID = B.CLE_ID AND A.CLE_CALLID = B.CLE_CALLID
                  INNER JOIN CL_APPID_XREF WITH (NOLOCK) on A.CLE_EVID = CLX_APPID
            WHERE A.CLE_TYPE = 508 AND B.CLE_TYPE = 536 and a.CLE_EVID = b.CLE_EVID
                  AND (A.CLE_EVID = 1 OR  A.CLE_EVID = 13)
            Group BY CLE_IDX_CHAN

Open in new window

0
Comment
Question by:portlight
5 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
Comment Utility
The two lines beginning with CONVERT are not valid aggregations for a group by statement:

CONVERT(VARCHAR(12),DATEADD(ss, AVG(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) ''Avg Length'',
0
 

Author Comment

by:portlight
Comment Utility
Those are not giving me an issue. It is when I add the

 MAX(CONVERT(VARCHAR(12),DATEADD(ss, SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) )

Open in new window


Line that I begin to get the problem.
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
Comment Utility
Hi ,

Try this:
select
	cs.CLE_IDX_CHAN
	, cs.callcount as 'Call Count'
	, cs.seconds as seconds
	, convert( varchar( 12 ), dateadd( ss, cs.AvgLength, 0 ), 8 ) as 'Average Length'
	, convert( varchar( 12 ), dateadd( ss, cs.seconds, 0 ), 8 ) as 'Total Length'
	, convert( varchar( 12 ), dateadd( ss, cs.MaxLength, 0 ), 8 ) as 'Max Length'
from (
	SELECT 
		CLE_IDX_CHAN
		, COUNT(CLE_IDX_OFFHOOK) 'CallCount'
		, SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)) 'Seconds'
		, AVG(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)) 'AvgLength'
		, max( datefidd( ss, cle_idx_offhook, cle_idx_onhook )) 'MaxLength'
	FROM CL_EVENTS_IDX WITH (NOLOCK)
	INNER JOIN CL_EVENTS A WITH (NOLOCK) 
		ON CLE_IDX_KEY = CLE_ID
	INNER JOIN UTILITY_INFO WITH (NOLOCK) 
		ON CLE_IDX_UCODE = UTILITY_KEY
	INNER JOIN CL_EVENTS B WITH (NOLOCK) 
		ON A.CLE_ID = B.CLE_ID 
		AND A.CLE_CALLID = B.CLE_CALLID
		and a.CLE_EVID = b.CLE_EVID
	INNER JOIN CL_APPID_XREF WITH (NOLOCK) 
		on A.CLE_EVID = CLX_APPID
	WHERE 
		A.CLE_TYPE = 508 
		AND B.CLE_TYPE = 536 
		AND (A.CLE_EVID = 1 OR  A.CLE_EVID = 13)
	Group BY 
		CLE_IDX_CHAN 
	) cs
;

Open in new window


What I've done:
Moved a line from the where clause to the join of cl_events
Moved the converts to varchar etc to an outer query
Left an inner query with the group by

HTH
  David

PS If you'd like it tested, please provide test data
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
<I am getting an aggregate error.>
please post the error
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> Line that I begin to get the problem.
MAX(CONVERT(VARCHAR(12),DATEADD(ss, SUM(DATEDIFF(ss, CLE_IDX_OFFHOOK, CLE_IDX_ONHOOK)), CAST(0 AS DATETIME)),8) ) <<

You can't do a MAX() of a SUM() in SQL Server; you can't embed any of the aggregate functions within each other.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now