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 2012 Forums
 Transact-SQL (2012)
 Dynamically calculate rolling months

Author  Topic 

NewSQL11
Starting Member

6 Posts

Posted - 2014-10-09 : 10:34:17
I have something that works but don't like all the unions to make it work.

Is it possible to turn this into a single query without unions?

Basically first union gives Rolling value for July - May.
Second Union gives Rolling value for June - April.


Declare @StartDate as datetime;
Declare @SelectedCompany as varchar(100);

set @Startdate = '7/1/2014';
set @SelectedCompany = 'Company';


select max(a.[Month_Year__Date_]) as "Month_Year__Date_",
(sum([Chat___Total_Time_To_Live_Agent__Avg_]*[Chat_____of_Successful_Interactions])/sum([Chat_____of_Successful_Interactions])) as Peer_ResponseSpeed_3MO
from
[ChatMetricsMonthly] a,
[ChatCustomMetricsMonthly] b,
[PeerMapping] c
where
a.[Month_Year__Date_] between dateadd(month, -2, @StartDate) and @StartDate
and a.[Company] = c.[competitor]
and a.company = b.company
and c.[subjectcompany] = @SelectedCompany
and a.[Month_Year__Date_] = b.month_year__date_
union
select dateadd(month, -1, @StartDate),
(sum([Chat___Total_Time_To_Live_Agent__Avg_]*[Chat_____of_Successful_Interactions])/sum([Chat_____of_Successful_Interactions])) as Peer_ResponseSpeed_3MO
from
[ChatMetricsMonthly] a,
[ChatCustomMetricsMonthly] b,
[PeerMapping] c
where
a.[Month_Year__Date_] between dateadd(month, -3, @StartDate) and dateadd(month, -1, @StartDate)
and a.[Company] = c.[competitor]
and a.company = b.company
and c.[subjectcompany] = @SelectedCompany
and a.[Month_Year__Date_] = b.month_year__date_

NewSQL11
Starting Member

6 Posts

Posted - 2014-10-09 : 14:18:33
Using a different query I had I did this. Any issues with this method? It seems much easier than unions I was doing before.


with ConsolidatedData as
(select a.[company], a.[Month_Year__Date_], a.[Email___Response_Time_To_Initial_Email__Avg_], [Email_____of_Successful_Interactions] from
[EmailMetricsMonthly] a,
[EmailCustomMetricsMonthly] b
where a.[Month_Year__Date_] = b.[Month_Year__Date_] and a.company = b.company)


select T1.[Month_Year__Date_] as Month,
sum(CASE WHEN t1.company <> @SelectedCompany THEN NULL ELSE (t2.[Email___Response_Time_To_Initial_Email__Avg_]*t2.[Email_____of_Successful_Interactions]) end)
/sum(CASE WHEN t1.company <> @SelectedCompany THEN NULL ELSE t2.[Email_____of_Successful_Interactions] END) AS ResponseSpeed_3MO,
sum(CASE WHEN b.Competitor IS NULL THEN NULL ELSE (t2.[Email___Response_Time_To_Initial_Email__Avg_]*t2.[Email_____of_Successful_Interactions]) end)
/sum(CASE WHEN b.Competitor IS NULL THEN NULL ELSE t2.[Email_____of_Successful_Interactions] END) AS "Peer Average"
from ConsolidatedData T2
LEFT JOIN [SeismicPeerMapping] AS b ON b.Competitor = T2.Company
and b.[SubjectCompany] = @SelectedCompany
join [EmailMetricsMonthly] T1
on
T2.[Month_Year__Date_] <= T1.[Month_Year__Date_]
and T2.[Month_Year__Date_] > dateadd(month,-3,T1.[Month_Year__Date_])
where T1.[Month_Year__Date_] between dateadd(month,-3,@StartDate) and @StartDate
and t1.company = t2.company
group by T1.[Month_Year__Date_]
order by T1.[Month_Year__Date_]
Go to Top of Page
   

- Advertisement -