我遇到以下问题的问题:
每月比较。将当月的销售额与其他三个数字进行比较: 一个。上个月。 湾前三个月的平均值。 C。前三个月前三个月的平均值。
这是数据库/多维数据集的结构:
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查询返回的所需结果如下:
请求帮助,我在互联网上尝试了很多不同的解决方案,但是它们没有返回正确的结果。
答案 0 :(得分:1)
以下查询应返回您想要的内容:
WITH MEMBER [Measures].[Current Month] AS
[Measures].[PIZZA SALES Count]
MEMBER [Measures].[Previous Month] AS
([TIME].[Month].CurrentMember.Lag(1),
[Measures].[PIZZA SALES Count])
MEMBER [Measures].[3 Months] AS
AVG([TIME].[Month].CurrentMember.Lag(3) : [TIME].[Month].CurrentMember.Lag(1),
[Measures].[PIZZA SALES Count])
MEMBER [Measures].[6 Months] AS
AVG([TIME].[Month].CurrentMember.Lag(6) : [TIME].[Month].CurrentMember.Lag(4),
[Measures].[PIZZA SALES Count])
SELECT
{
[Measures].[Current Month],
[Measures].[Previous Month],
[Measures].[3 Months],
[Measures].[6 Months]
}
ON COLUMNS,
{
[TIME].[Month].&[12]
} ON ROWS
FROM [Pizza Place Cube]
请注意Avg
需要一个set作为第一个参数,冒号结构构建一个以冒号前面的成员开头并以后面的成员结束的集合。