Hey everyone . . .I have been working on this for a day and a half now, and am coming up against a deadline, so I thought I would throw it out to the experts. The purpose of this query is to create a straight line for a graph to display a baseline average, but I am not calculating the correct type of average. . . In the below query I am calculating two types of averages. In the sub-query I am calculating the weighted average and in the main top level select I am doing a simple average calculation. What I need to do is calculate a weighted average in the top level as well, and I am struggling with how I can get the appropriate values for the calculation.The weighted average should look somthing like this:(([Average Elapsed Time]*Transactions)/Transactions)
Similar to this snippet: (avg(duration) *count(duration)/count(duration))
Any thoughts on how I can integrate the weighted average into the main select as well?Thanks Everyone!SELECT Client_Mnemonic, AVG(CASE WHEN Seq <= (4) and BarType != 'Trending' THEN [Average Elapsed Time] ELSE NULL END) OVER (PARTITION BY Client_Mnemonic,Group_type) AS AvgTime,[Work Week],Group_typeFROM(SELECT *,ROW_NUMBER() OVER (PARTITION BY Client_Mnemonic,Group_Type ORDER BY [Work Week]) AS Seq,([Average Elapsed Time] * [Transactions]) as "Total"FROM(SELECT Client_Mnemonic, round((avg(duration) *count(duration)/count(duration)),0) As "Average Elapsed Time", count(workflow_id) as "Transactions",CONVERT(varchar(50), (DATEADD(d, @@DATEFIRST - DATEPART(dw, Start_Time) - 5, Start_Time)),101) As "Work Week", Group_TypeFROM workflow_dataWHEREClient_mnemonic = ('XXXX_XX') and (Group_type = 'Pilot') and duration <= 1800 and(definition = 'Orders - Individual Order Search' and datepart(dw, workflow_data.Start_Time) not in (1,7))GROUP BY datepart(ww,workflow_data.start_time), Client_Mnemonic, CONVERT(varchar(50), (DATEADD(d, @@DATEFIRST - DATEPART(dw, Start_Time) - 5, Start_Time)), 101), Group_Type )q)rinner join (Select [Work_Week], CASE WHEN Seq1 <= 4 THEN 'Baseline' ELSE 'Trending' END as "BarType"FROM(SELECT CONVERT(varchar(50), (DATEADD(d, @@DATEFIRST - DATEPART(dw, Start_Time) - 5, Start_Time)),101) as "Work_Week",ROW_NUMBER() OVER (ORDER BY CONVERT(varchar(50), (DATEADD(d, @@DATEFIRST - DATEPART(dw, Start_Time) - 5, Start_Time)),101)) AS Seq1FROM workflow_datawhere client_mnemonic = ('XXXX_XX') and group_type = 'Pilot' group by CONVERT(varchar(50), (DATEADD(d, @@DATEFIRST - DATEPART(dw, Start_Time) - 5, Start_Time)),101) )m)ion r.[Work Week]=i.Work_Week