0

我对以下查询有疑问:

逐月比较。将当月的销售额与其他三个数字进行比较:上个月。湾。前三个月的平均值。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 查询返回的所需结果如下: 在此处输入图像描述

请帮忙,我在互联网上尝试了许多不同的解决方案,但是它们没有返回正确的结果。

4

1 回答 1

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需要一个集合作为第一个参数,并且冒号构造构建一个集合,该集合从冒号之前的成员开始,以冒号之后的成员结束。

于 2013-10-08T19:20:23.477 回答