在我的主查询中,我有一个子查询来返回12周内的平均净销售额,但由于餐厅因装修或扩建而关闭,这会使价值扭曲。
如何更改查询以不计算net为空的日期?
SELECT System_RestaurantDateJoined.StoreKey, System_RestaurantDateJoined.RestaurantName, System_RestaurantDateJoined.Ownership
,System_RestaurantDateJoined.LeaseEagle_ID, System_RestaurantDateJoined.State, System_RestaurantDateJoined.DateKey,
CASE WHEN DimLFL.Commentary = 'New Rest' THEN 'Category 1 - New Rest as flagged in LFL File'
WHEN DimLFL.Commentary = 'Mjr Reno' THEN 'Category 2 - Reno Rest as flagged in LFL File'
WHEN DimLFL.Commentary = 'Min Reno' THEN 'Category 2 - Reno Rest as flagged in LFL File'
WHEN DimLFL.Commentary = 'Temp Close' THEN 'Category 3 - Temp Closure'
WHEN ISNULL(Daily_Sales_Summary.Net, 1) <= 1 THEN 'Category 4 - Missing Data'
WHEN ISNULL(Daily_Sales_Summary.LastYear_Net, 1) = 1 THEN 'Category 6 - No sales last year'
WHEN ISNULL(Daily_Sales_Summary.[LastWeek_Net],1) <= 1 THEN 'Category 7 - Missing Data for Last Week'
ELSE 'Category 5 - Potential Over/Under stated sales'
END as Category
,Daily_Sales_Summary.Net, Daily_Sales_Summary.[LastWeek_Net], Daily_Sales_Summary.LastYear_Net
,ISNULL(Daily_Sales_Summary.[12Week_Net], (SELECT AVG(Net) AS AVGNET
FROM (SELECT TOP (12) Net
FROM FactBankingAndDailySummary AS FB INNER JOIN
DimDate ON FB.DateKey = DimDate.DateKey
WHERE (FB.StoreKey = System_RestaurantDateJoined.StoreKey) AND (FB.DateKey <= System_RestaurantDateJoined.DateKey) AND
(DimDate.DayNameOfWeek = System_RestaurantDateJoined.DayNameOfWeek) AND (FB.DateKey >= (FB.DateKey - 84))
ORDER BY FB.DateKey, StoreKey DESC) AS WKAV)) AS [12Week_Net]
,Daily_Sales_Summary.TransactionCount,Daily_Sales_Summary.[LastWeek_TransactionCount]
,Daily_Sales_Summary.LastYear_TransactionCount, Daily_Sales_Summary.[12Week_TransactionCount], System_RestaurantDateJoined.FullDate
,System_RestaurantDateJoined.DayNameOfWeek, System_RestaurantDateJoined.FinancialWC, System_RestaurantDateJoined.strFinancialWeek
,DimLFL.Commentary
FROM Daily_Sales_Summary RIGHT OUTER JOIN
System_RestaurantDateJoined LEFT OUTER JOIN
DimLFL ON System_RestaurantDateJoined.StoreKey = DimLFL.StoreKey AND System_RestaurantDateJoined.DateKey = DimLFL.DateKey ON
Daily_Sales_Summary.RedCatID = System_RestaurantDateJoined.StoreKey AND
Daily_Sales_Summary.DateKey = System_RestaurantDateJoined.DateKey
答案 0 :(得分:0)
将平均值实现为Sum(Value) * 1.0 / Count([something])
,其中[something]
是一个表达式,当数据不应在分母中计算时返回null。如果Value
在没有数据时为空,那么它只是Sum(Value) * 1.0 / Count(Value)
。