Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query woe's

Author  Topic 

rbroersma
Starting Member

2 Posts

Posted - 2007-08-06 : 15:34:47
Hi,
I thought this would be simple, but I can't quite figure it out (I'm sure it is simple for someone a little more talented than me in this area)....

Basically I'm trying to run a query for multiple days. I first want to group the query by day. Then, for each day, I want to extract four 'call types [db field]', and create 1 row for each call type where the 'LocalQ [db field]' time is the largest for that call type for that day....

Sample output (that I'd like to see)...

Aug 6, 2007
calltype1 600
calltype2 660
calltype3 300
calltype4 250

Aug 7, 2007
calltype1 350
calltype2 300
.
.
.


Here is the query I currently have...

select  (TCD.LocalQTime), substring(convert (varchar(20),TCD.DateTime),1,11), CT.CallTypeID from Termination_Call_Detail TCD
join Call_Type CT on TCD.CallTypeID = CT.CallTypeID
where DateTime > '08/03/2007'
and DateTime < '08/06/2007'
and CT.CallTypeID in (5017,5018,5019,5020)
group by substring(convert (varchar(20),TCD.DateTime),1,11), TCD.LocalQTime, CT.CallTypeID
order by substring(convert (varchar(20),TCD.DateTime),1,11),CT.CallTypeID, TCD.LocalQTime desc


This sort of gives me what I want....

289 Aug 4 2007 5018
29 Aug 4 2007 5018
0 Aug 4 2007 5018
575 Aug 4 2007 5019
533 Aug 4 2007 5019
312 Aug 4 2007 5019
280 Aug 4 2007 5019
222 Aug 4 2007 5019
31 Aug 4 2007 5019
0 Aug 4 2007 5019
0 Aug 5 2007 5018
173 Aug 5 2007 5019
37 Aug 5 2007 5019
3 Aug 5 2007 5019
0 Aug 5 2007 5019
0 Aug 5 2007 5020


but it gives me all the records and I only want the largest record for each call type.

Any help would be appreciated.

Thanx,
Ryan.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-08-06 : 15:42:08
Use the MAX aggregate function on the column that you want to group, then remove that column from the GROUP BY.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

rbroersma
Starting Member

2 Posts

Posted - 2007-08-06 : 16:45:33
That worked perfect... Thanx for your help
Go to Top of Page
   

- Advertisement -