试图按工作日平均找到12周平均值

时间:2016-07-08 05:01:42

标签: sql sql-server database sql-server-2008-r2

我试图在同一个工作日找到12周的平均值。

示例:

weekday sale

所以基本上我需要基于net>商店数量的平均值。 0.对于黄色,它应该是总数/ 11,因为02-07-16没有销售

我已经写过这个查询,但它没有正常工作。你能帮助我吗?

SELECT   Store_DateJoined.StoreKey, Store_DateJoined.RestaurantName,  Store_DateJoined.Ownership, Store_DateJoined.DateKey, 

Sales_Summary.Net, Sales_Summary.[LastWeek_Net],

(SELECT  AVG(Net) AS AVGNET
FROM   (SELECT     TOP (12) Net
                   FROM  DailySummary AS FB INNER JOIN
                  DimDate ON FB.DateKey = DimDate.DateKey
                   WHERE  datename(WEEKDAY, TransactionDate) IN (select datename(WEEKDAY, TransactionDate) from [NandosDW].[dbo].[DailySummary]) and   (FB.StoreKey = Store_DateJoined.StoreKey) AND (FB.DateKey <= Store_DateJoined.DateKey) AND 
                                          (DimDate.DayNameOfWeek = Store_DateJoined.DayNameOfWeek) AND (FB.DateKey >= (FB.DateKey - 84))

                   ORDER BY FB.DateKey, StoreKey DESC) AS WAVGNET) AS [Week12_Net], 
                   Store_DateJoined.FullDate, Store_DateJoined.DayNameOfWeek, Store_DateJoined.FinancialWC, Store_DateJoined.strFinancialWeek

FROM   Sales_Summary RIGHT OUTER JOIN Store_DateJoined LEFT OUTER JOIN 
   DimLFL ON Store_DateJoined.StoreKey = DimLFL.StoreKey AND Store_DateJoined.DateKey = DimLFL.DateKey ON 
   Sales_Summary.RedCatID = Sore_DateJoined.StoreKey AND 
   Sales_Summary.DateKey = Store_DateJoined.DateKey

我没有在此查询中包含联接,但它们存在

0 个答案:

没有答案