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.
| Author |
Topic |
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-09-09 : 09:11:46
|
| I have a table structure like the followingID | Secs | DateInsertedSELECT count(ID) as AllCalls ,SUM(CASE WHEN Secs > 0 THEN 1 ELSE 0 END) as Answered ,SUM(CASE IsNull(Secs,0) WHEN 0 THEN 1 ELSE 0 END) as Unanswered ,DATEPART(hour, DateInserted) as theHour FROM MyTable WHERE convert(varchar,DateInserted,111) = convert(varchar,getdate(),111) GROUP BY DATEPART(hour, DateInserted) ORDER BY DATEPART(hour, DateInserted) ASCAnd the above SQL gets me all the calls for the current day, grouped by hour.What I need to is take this query and add to so that I can get all the same data but get the average for same day in the last 6 weeks.So, if I were running the report today, I would want to get the above data, but the average of every Tuesday for the last six weeksAnd I also want to get the average per week for the last six weeks. Any help would be appreciated. Thanks alotHC |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 09:21:17
|
This can get you startedSELECT COUNT(ID) AS AllCalls, SUM(CASE WHEN ISNULL(Secs, 0) > 0 THEN 1 ELSE 0 END) as Answered, SUM(CASE WHEN ISNULL(Secs, 0) = 0 THEN 1 ELSE 0 END) as UnansweredFROM MyTableWHERE DateInserted >= DATEDIFF(DAY, '19000212', GETDATE()) AND DateInserted < DATEDIFF(DAY, '18991231', GETDATE()) AND DATENAME(WEEKDAY, DateInserted) = 'Tuesday' E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-09-09 : 11:20:15
|
| In the scenario above, how do I get the AVERAGE for each of the rows, still grouped by hourSELECT COUNT(ID) AS AllCalls, SUM(CASE WHEN ISNULL(Secs, 0) > 0 THEN 1 ELSE 0 END) as Answered, SUM(CASE WHEN ISNULL(Secs, 0) = 0 THEN 1 ELSE 0 END) as UnansweredFROM MyTableWHERE DateInserted >= DATEDIFF(DAY, '19000212', GETDATE()) AND DateInserted < DATEDIFF(DAY, '18991231', GETDATE()) AND DATENAME(WEEKDAY, DateInserted) = 'Tuesday'I need to get the avergage of AllCalls, Answered, Unanswered. Thanks.... |
 |
|
|
Harry C
Posting Yak Master
148 Posts |
Posted - 2008-09-09 : 12:28:51
|
| Ok, this gets me part of the way. The only problem now is I dont know how to group the items the right way. See the bolded part below. How can I still get the display to be likeAny help is appreciated, I am under a tight deadline. Thanks!AllCallsAvg AnsweredAvg UnansweredAvg theHour14 1 13 014 2 12 17 0 7 24 0 4 31 0 1 42 0 2 54 0 4 67 0 7 7SELECT CAST(AVG(theCount) as numeric(10,2)) as AvgCount FROM(SELECT CAST(Count(*) as numeric(10,2)) as theCountFROM MyTable WHERE convert(varchar,DateInserted,111) >= convert(varchar,dateadd(week, datediff(week, 0, getdate()) - 6, 0),111) AND DATENAME(WEEKDAY, DateInserted) = DATENAME(WEEKDAY, getdate())AND DATEPART(hour, DateInserted) = 0GROUP BY DATEPART(week, DateInserted) ) a |
 |
|
|
|
|
|
|
|