MDX - 如何根据事实表中存在的顺序天来计算维数?

时间:2017-03-21 20:17:40

标签: ssas mdx

我需要获得符合两个标准的学生数量:

  1. 在最后3个学年时有3个顺序缺席,或者
  2. 在过去2个月内没有5次顺序缺席。
  3. 我有白天学生缺席的事实,其中包括以下字段:

    • StudentId
    • SchoolCalendarId
    • SchoolId
    • CLASSID
    • QtyAbsents(总是1,只是总和)

    尺寸:

    • DimStudent
    • DimSchoolCalendar
    • DimSchool
    • DimClass

    我做了一个MDX,返回符合第一个标准的学生名称:

    WITH
        MEMBER [A] AS 
            CASE
                WHEN [Measures].[QtyLackingStudents] = 3 
                THEN 'THREE SEQUENCIAL ABSENTS' ELSE NULL END
    SELECT
        NON EMPTY { [A] } ON COLUMNS,
        NON EMPTY { [DimStudent].[Name].ALLMEMBERS } ON ROWS
    FROM
    (
        SELECT
            ( FILTER(
                [DimSchoolCalendar].[Date].MEMBERS,
                [DimSchoolCalendar].[Date].CURRENTMEMBER.NAME = VBAMDX!Format(VBAMDX!Now(),"yyyy-MM-dd")
            ).ITEM(0).Lag(3):FILTER(
                [DimSchoolCalendar].[Date].MEMBERS,
                [DimSchoolCalendar].[Date].CURRENTMEMBER.NAME = VBAMDX!Format(VBAMDX!Now(),"yyyy-MM-dd")
            ).ITEM(0) ) ON COLUMNS
        FROM 
            (
                SELECT 
                    ( { [DimSchoolCalendar].[SchoolDay].&[IsSchoolDay] } ) ON COLUMNS
                FROM [AlunosMatriculados]
            )
    )
    

    但我既不能做第二个标准,也不能把所有内容都放在一个单一的标准中。

    修改

    我制作了一个返回我想要的SQL。我需要MDX。

    SELECT COUNT(DISTINCT f.StudentId)
    FROM DW.FactLackingStudents f
        INNER JOIN DW.DimSchool e ON e.SchoolId = f.SchoolId
        INNER JOIN DW.DimSchoolCalendar ce ON ce.SchoolCalendarId = f.SchoolCalendarId
    WHERE (SELECT COUNT(*) 
            FROM DM.FactLackingStudents f2
                INNER JOIN (
                    SELECT c.SchoolCalendarId
                        , ROW_NUMBER() OVER(PARTITION BY c.Year, c.SchoolId ORDER BY c.Date DESC) AS Seq
                    FROM dm.DimCalendarioEscolar c
                    WHERE c.Year = a.Year
                        and c.SchoolKey = e.SchoolKey
                        and c.Date < GETDATE()
                        and c.SchoolDay = 'IS SCHOOL DAY'
                ) days ON days.SchoolCalendarId = f2.SchoolCalendarId
                    and days.Seq <= 3
            WHERE f2.StudentId = f.StudentId) = 3   -- ABSENTS AT LAST 3 SCHOOL DAYS IS EQUAL 3
        OR 
        (
            SELECT COUNT(*) 
            FROM DM.FactLackingStudents f2
                INNER JOIN (
                    SELECT c.SchoolCalendarId
                        , ROW_NUMBER() OVER(PARTITION BY c.Year, c.SchoolId ORDER BY c.Date DESC) AS Seq
                    FROM dm.DimCalendarioEscolar c
                    WHERE c.Year = a.Year
                        and c.SchoolKey = e.SchoolKey
                        and c.Date BETWEEN DATEADD(DD, -60, GETDATE()) AND GETDATE() -- LAST 2 MONTHS
                        and c.SchoolDay = 'IS SCHOOL DAY'
                ) days ON days.SKCalendarioEscolar = f2.SKDataInfrequencia
            WHERE f2.StudentId = f.StudentId
        ) >= 5  -- ABSENTS AT LAST 60 DAYS IS EQUAL 5
    

1 个答案:

答案 0 :(得分:1)

一步一步:

第一个标准

创建一个从开头到今天所有日子的日期集:

,

将该套装与IsSchoolDay交叉加入,以便仅限上学日:

{NULL:StrToMember('[DimSchoolCalendar].[Date].&[' + Format(Now(),'yyyy-MM-dd') + ']')}

从集合中获取最后三个成员:

[DimSchoolCalendar].[SchoolDay].&[IsSchoolDay] * {NULL:StrToMember('[DimSchoolCalendar].[Date].&[' + Format(Now(),'yyyy-MM-dd') + ']')}

创建计算度量:

Tail(               
    [DimSchoolCalendar].[SchoolDay].&[IsSchoolDay] * {NULL:StrToMember('[DimSchoolCalendar].[Date].&[' + Format(Now(),'yyyy-MM-dd') + ']')},
    3
)

第二个标准

获取当前月份:

SUM(    
    Tail(           
        [DimSchoolCalendar].[SchoolDay].&[IsSchoolDay] * {NULL:StrToMember('[DimSchoolCalendar].[Date].&[' + Format(Now(),'yyyy-MM-dd') + ']')},
        3
    ),
    [Measures].[QtyLackingStudents]
)

过去两个月:

StrToMember('[DimSchoolCalendar].[Month].&[' + Format(Now(),'yyyy-MM') + ']')

创建计算度量:

LastPeriods(
    2,
    StrToMember('[DimSchoolCalendar].[Month].&[' + Format(Now(),'yyyy-MM') + ']')
)

最终结果

SUM(    
    LastPeriods(
        2,
        StrToMember('[DimSchoolCalendar].[Month].&[' + Format(Now(),'yyyy-MM') + ']')
    ),
    [Measures].[QtyLackingStudents]
)