过滤的DistinctCount度量和MDX不提供与SQL相同的结果

时间:2017-02-24 21:06:15

标签: sql sql-server ssas mdx sql-server-2016

我有以下两个查询,一个SQL,一个MDX:

SQL:

SELECT t.term_report_year, COUNT(*)
FROM(
     SELECT DISTINCT de.term_report_year, fe.student_id
     FROM warehouse.FactEnrolments fe
     INNER JOIN warehouse.DimDate dd
     ON fe.term_record_creation_fk = dd.DateKey
     INNER JOIN warehouse.DimTermEnrolments de
     ON fe.term_enrolments_fk = de.term_enrolments_pk
     WHERE dd.ISOWeekNumberOfYear <= 8 OR dd.ISOYearCode < de.term_report_year
) t
GROUP BY t.term_report_year
ORDER BY term_report_year

MDX:

SELECT 
  NON EMPTY 
    Measures.[Enrolments] ON COLUMNS
 ,NON EMPTY 
      Filter
      (
            [Term Enrolments].[Term Year].Children *
            [Term Record Creation].[ISO Year Code].children *
            [Term Record Creation].[ISO Week Number Of Year].children
       ,
        Cint([Term Record Creation].[ISO Week Number Of Year].CurrentMember.Member_Key) <= 8
        OR
        Cint([Term Record Creation].[ISO Year Code].CurrentMember.Member_key) < Cint([Term Enrolments].[Term Year].CurrentMember.Member_key)
      ) ON ROWS
FROM [Enrolments];

我试图在两者中表达这个想法,“计算一年中在当年第8周之前或期间注册的学生人数”year = term_year

在我的SSAS多维数据集中,Enrolments指标是DistinctCount上的student_id。在SQL查询中,term_report_year相当于Term Year中的MDX

有人可以解释为什么这两个查询没有提供相同的数字,例如2016年SQL给出2803和MDX 2948?

我认为这与几个星期的MDX重复计算有关,但我无法解决如何解决它。

1 个答案:

答案 0 :(得分:3)

试试这个。我希望它能将年度总数过滤到几周<= 8

SELECT 
  NON EMPTY 
    Measures.[Enrolments] ON COLUMNS
 ,NON EMPTY  [Term Enrolments].[Term Year].Children ON ROWS
FROM (
 SELECT
      Filter
      (
            [Term Enrolments].[Term Year].Children *
            [Term Record Creation].[ISO Year Code].children *
            [Term Record Creation].[ISO Week Number Of Year].children
       ,
        Cint([Term Record Creation].[ISO Week Number Of Year].CurrentMember.Member_Key) <= 8
        OR
        Cint([Term Record Creation].[ISO Year Code].CurrentMember.Member_key) < Cint([Term Enrolments].[Term Year].CurrentMember.Member_key)
      ) ON COLUMNS
  FROM [Enrolments]
);