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 |
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-20 : 18:52:32
|
So I have the following query:Select Year(DATEADD(MONTH,3,date)) as Year, Month(DATEADD(MONTH,3,date)) as Month, location, COUNT (agent_login_id)from Agentswhere Location = 'syd'Group by Location, Year(DATEADD(MONTH,3,date)), Month(DATEADD(MONTH,3,date)) it outputs:Year Month location Count2013 1 SYD 12013 3 SYD 112013 4 SYD 22013 5 SYD 22013 8 SYD 32013 9 SYD 12013 10 SYD 42013 11 SYD 72013 12 SYD 72014 1 SYD 32014 2 SYD 1But I need to do a cumulative count so for example this is what I need it to look likeYear Month location Count Cumulative count2013 1 SYD 1 12013 3 SYD 11 122013 4 SYD 2 14How can I do this? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-11-20 : 21:59:53
|
[code];with cte as( select [date] = dateadd(month, datediff(month, 0, [date]) + 3, 0), location, cnt = count(*) from Agents a where location = 'SYD' group by dateadd(month, datediff(month, 0, [date]) + 3, 0), location)select *from cte a cross apply ( select Cumulative = count(*) from Agents x where x.location = a.location and dateadd(month, datediff(month, 0, x.[date]) + 3, 0) <= a.[date] ) c[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-20 : 22:35:17
|
Genius. thank you! |
|
|
2revup
Posting Yak Master
112 Posts |
Posted - 2013-11-20 : 23:02:54
|
Now I have what is likely a harder barer to overcome, I need to include the above with another query. As I need the query to use the CTE output as a count. I have 2 queries that I need to put together but need it to use the results of one of them to give me the intended output. So in brief I am trying to find out the AVG of each month, to do this I need to divide by the users. The problem is the users varies from month to month, so I just cant take the number from today, as the result will have varied from month to month and never be constant.The first query gives me a result of how many staff we have for that month. ;with Cumulative_cte as( select date = dateadd(month, datediff(month, 0, date) + 3, 0), location, cnt = count(*) from Agents a where location = 'SYD' group by dateadd(month, datediff(month, 0, date) + 3, 0), location)select *from Cumulative_cte a cross apply ( select Cumulative = count(*) from Agents x where x.location = a.location and dateadd(month, datediff(month, 0, x.date) + 3, 0) <= a.date ) cUsing that I need to incorporate that into this query, which is doing a division by the engineer, so it needs to do the division by the engineer of the matching month. here is the query that I need to get to use that CTE result.Select Correspondances.Year,Correspondances.Month,a.location,correspondances.Corr_Count, Phone.CallNo, Chat.ChatNo, Forums.ForumPosts,sum(correspondances.Corr_Count + Phone.CallNo + Chat.ChatNo + ISNULL(Forums.ForumPosts, 0)) / count(distinct a.agent_login_id),(sum(correspondances.Corr_Count + Phone.CallNo + Chat.ChatNo + ISNULL(Forums.ForumPosts, 0)) / count(distinct a.agent_login_id)) / count(distinct a.agent_login_id) as AVGPerEngineer ,count(distinct a.agent_login_id) as AgentsOnSitefrom (Select Year(comm_date_utc) as Year, Month(comm_date_utc) as Month, location, Count(comm_id) as Corr_CountfromCorrespondencesleft join Agents on owner_agent_login = agent_login_idwhere location in ('SYD')and comm_date_utc between '2013/01/01 00:00:00' and '2013/10/31 23:59:00'and comm_date_utc between DATEADD(day,90,Date) and '2013/10/31 23:59:00' group by Year(comm_date_utc) , Month(comm_date_utc), location) CorrespondancesLeft outer join(select Year(STATISTIC_DATE) as Year, Month(STATISTIC_DATE) as Month, location, sum(tot_calls_handled) as CallNofromPhoneandChat left join Agents on agent_login = agent_login_idwhere location in ('SYD')and STATISTIC_DATE between '2013/01/01 00:00:00' and '2013/10/31 23:59:00' and STATISTIC_DATE between DATEADD(day,90,Date) and '2013/10/31 23:59:00' and TOT_CALLS_HANDLED >= 0and phone_skill not LIKE '%Chat%'group by Year(STATISTIC_DATE),Month(STATISTIC_DATE), location) Phoneon Correspondances.location = Phone.Locationand Correspondances.Year = Phone.Yearand Correspondances.Month = Phone.MonthLeft outer join(select Year(STATISTIC_DATE) as Year, Month(STATISTIC_DATE) as Month, location, sum(tot_calls_handled) as chatNofromPhoneandChat left join Agents on agent_login = agent_login_idwhere location in ('SYD')and STATISTIC_DATE between '2013/01/01 00:00:00' and '2013/10/31 23:59:00' and STATISTIC_DATE between DATEADD(day,90,Date) and '2013/10/31 23:59:00' and TOT_CALLS_HANDLED >= 0group by Year(STATISTIC_DATE),Month(STATISTIC_DATE), location) Chaton Correspondances.Location = chat.Locationand Correspondances.Year = chat.Yearand Correspondances.Month = chat.MonthLeft outer join(Select Year(Posted_Date_utc) as Year, Month(Posted_Date_utc) as Month, location, ISNULL(count(login), 0) as ForumPosts from Forums left join Agents on login = agent_login_idwhere location in ('SYD')and Posted_Date_utc between '2013/01/01 00:00:00' and '2013/10/31 23:59:00' and Posted_Date_utc between DATEADD(day,90,Date) and '2013/10/31 23:59:00'group by Year(Posted_Date_utc),Month(Posted_Date_utc), location) Forumson Correspondances.Location = Forums.Locationand Correspondances.Year = forums.Yearand Correspondances.Month = Forums.Monthjoin (select distinct location, agent_login_id, title from Agents) a on Correspondances.Location = a.Locationgroup by Correspondances.Year, Correspondances.Month,a.Location, correspondances.Corr_Count, phone.callNo,Chat.chatNo, Forums.ForumPostsorder by YEAR, MONTH, location |
|
|
|
|
|
|
|