0

次のクエリに問題があります。

月ごとの比較。当月の売上を他の 3 つの数値と比較します。前月。b. 過去 3 か月の平均。c. 過去 3 か月前の 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