我对以下查询有疑问:
逐月比较。将当月的销售额与其他三个数字进行比较:上个月。湾。前三个月的平均值。C。前 3 个月前三个月期间的平均值。
这是数据库/多维数据集的结构:
WITH MEMBER [Measures].[Current Month] AS
[Measures].[PIZZA SALES Count]
MEMBER [Measures].[Previous Month] AS
AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(1)},[Measures].[PIZZA SALES Count]])
MEMBER [Measures].[3 Months] AS
((AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(1)},[Measures].[PIZZA SALES Count]]) +
AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(2)},[Measures].[PIZZA SALES Count]]) +
AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(3)},[Measures].[PIZZA SALES Count]]))/3)
MEMBER [Measures].[6 Months] AS
((AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(4)},[Measures].[PIZZA SALES Count]]) +
AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(5)},[Measures].[PIZZA SALES Count]]) +
AVG({[PIZZA SALES].[Time ID].Children * [TIME]. [Month].CurrentMember.lag(6)},[Measures].[PIZZA SALES Count]]))/3)
SELECT
{
[Measures].[Current Month],
[Measures].[Previous Month],
[Measures].[3 Months],
[Measures].[6 Months]
}
ON COLUMNS,
{
[TIME].[Month].&[12]
} ON ROWS
FROM [Pizza Place Cube];
此查询的结果如下:
以下 SQL 脚本返回正确的结果:
SELECT
(SELECT COUNT(*) FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES))) AND
Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM PIZZA_SALES)))) AS 'CURRENT MONTH',
(SELECT COUNT(*) FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-1 AND
Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM PIZZA_SALES)))) AS 'PREVIOUS MONTH',
(SELECT COUNT(*)/3 FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE
Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM PIZZA_SALES)) AND
(Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-1 OR
Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-2 OR
Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-3))) AS '3 MONTHS',
(SELECT COUNT(*)/3 FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE
Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM PIZZA_SALES)) AND
(Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-4 OR
Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-5 OR
Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM PIZZA_SALES)))-6))) AS '6 MONTHS';
SQL 查询返回的所需结果如下:
请帮忙,我在互联网上尝试了许多不同的解决方案,但是它们没有返回正确的结果。