过去6个等效MTD中的天数集

时间:2015-06-23 16:41:34

标签: ssas mdx olap

想象一下今天是2010年6月22日(我已经使用了这个日期AdvWrks立方体已经过时了)

我想找到本月组成的日子,即6月1日至6月22日加上迄今为止5个月内的日子。

所以这些天会构成一套:

(2010年1月1日 - 2010年1月22日)+
(2010年2月1日 - 2010年2月22日)+
(2010年3月1日 - 2010年3月22日)+
(2010年4月1日 - 2010年4月22日)+
(2010年5月1日 - 2010年5月22日)+
(2010年6月1日 - 2010年6月22日)

以下给出了132天的这一套:

WITH 
  SET [Days in Current Month] AS 
     [Date].[Calendar].[Date].&[20100601]:[Date].[Calendar].[Date].&[20100622]
  SET [Mths in Past 6 Mths] AS 
    Tail
    (
      [Date].[Calendar].[Month].MEMBERS
     ,6
    ) 
  SET [Prev Equiv MTDs] AS 
    Generate
    (
      [Mths in Past 6 Mths]
     ,Head
      (
        Descendants
        (
          [Mths in Past 6 Mths].CurrentMember
         ,[Date].[Calendar].[Date]
         ,SELF
        )
       ,[Days in Current Month].Count
      )
    ) 
SELECT 
  {} ON 0
 ,[Prev Equiv MTDs] ON 1
FROM [Adventure Works];

是否可以在不使用Generate功能的情况下找到这一天?

1 个答案:

答案 0 :(得分:1)

这是一种方式:

WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]


SELECT {} ON 0,
//CurrentMonth
    {   
        FirstDateOfMonth.ITEM(0)
        :
        LastDayOfMonth.ITEM(0)
    }

+
//-1 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        1,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        1,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-2 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        2,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        2,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-3 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        3,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        3,
                        LastDayOfMonth.ITEM(0)
                      )

    }   
+
//-4 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        4,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        4,
                        LastDayOfMonth.ITEM(0)
                      )

    }   

+
//-5 month
    {
        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        5,
                        FirstDateOfMonth.ITEM(0)
                      )
         :

        ParallelPeriod(
                        [Date].[Calendar].[Month],
                        5,
                        LastDayOfMonth.ITEM(0)
                      )

    }   

 ON 1
FROM [Adventure Works]

还有一个:

SELECT 
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.FirstChild
        :
        COUSIN
        (
        [Date].[Calendar].[Date].&[20100622], 
        [Date].[Calendar].[Date].&[20100622].PARENT
        )

    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(1).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(1)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(2).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(2)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(3).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(3)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(4).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(4)
              )
    }
    +
    {
        [Date].[Calendar].[Date].&[20100622].PARENT.LAG(5).FirstChild
        :
        COUSIN
              (
                [Date].[Calendar].[Date].&[20100622], 
                [Date].[Calendar].[Date].&[20100622].PARENT.LAG(5)
              )
    }
    ON 1,
{} ON 0
FROM [Adventure Works]

还有一个......

WITH SET FirstDateOfMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET LastDayOfMonth AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfMonth.ITEM(0):LastDayOfMonth.ITEM(0)}.COUNT

SET Dates AS 
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(1).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(2).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(3).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(4).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )
        +
    HEAD(
        [Date].[Calendar].[Date].&[20100622].Parent.LAG(5).ITEM(0).CHILDREN,
        Measures.PositionOfDate
        )


SELECT Dates ON 0,
{} ON 1
FROM [Adventure Works]

也是这样:

WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100622].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100622]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT

SELECT

        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(5).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(4).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(3).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(2).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0).PARENT.lag(1).FirstChild.LEAD(Measures.PositionOfDate - 1)
                    )  
                    +
        PeriodsToDate
                    (
                        [Date].[Calendar].[Month],
                        SelectedDate.ITEM(0)
                    ) 

ON 1,
{} ON 0
FROM [Adventure Works]

修改

包含非重复块的慢代码:

WITH SET FirstDateOfSelectedMonth AS [Date].[Calendar].[Date].&[20100601].Parent.FirstChild
SET SelectedDate AS [Date].[Calendar].[Date].&[20100601]
MEMBER Measures.PositionOfDate AS {FirstDateOfSelectedMonth.ITEM(0):SelectedDate.ITEM(0)}.COUNT
MEMBER Measures.PositionOfCurrentDate AS 
([Date].[Calendar].currentmember.Parent.FirstChild:[Date].[Calendar].currentmember).COUNT

SELECT 
    {
    [Date].[Calendar].[Date].&[20100601].Parent.LAG(5).FirstChild.ITEM(0)
    :
    [Date].[Calendar].[Date].&[20100601]
    }
    HAVING Measures.PositionOfCurrentDate <= Measures.PositionOfDate
ON 1,
{} ON 0
FROM [Adventure Works]