MDX - 如何根据维度中的开始和结束日期按日期聚合
MDX - How to aggregate by date considering start and end date in a dimension
考虑到维度中的开始日期和结束日期,我需要在日期范围内计算按日期分组的度量。 objective是统计一段时间内某学校每天活跃的学生人数
以下 SQL 代码适用于数据仓库(多维数据集的来源)。
SELECT
t.[date]
,count(distinct a.SKStudent) as Students
FROM DM.DimTime t
CROSS JOIN DM.FactStudents a
INNER JOIN DM.DimStudents m ON a.SKStudent = m.SKStudent
INNER JOIN DM.DimSchool e ON a.SKSchool = e.SKSchool
WHERE t.[date] between '20170502' and '20170512'
and e.SchoolCode = 123456
and t.[date] between m.[StartDate] and m.[EndtDate]
GROUP BY t.[data]
结果集如下:
+--------------+----------+
| date | Students |
+--------------+----------+
| 2017-05-02 | 567 |
| 2017-05-03 | 567 |
| 2017-05-04 | 568 |
| 2017-05-05 | 570 |
| 2017-05-06 | 570 |
| 2017-05-07 | 570 |
| 2017-05-08 | 573 |
| 2017-05-09 | 573 |
| 2017-05-10 | 571 |
| 2017-05-11 | 568 |
| 2017-05-12 | 568 |
+--------------+----------+
我试着做了MDX代码。以下是我尝试过的。我不知道如何使用 CASE 语句中的逻辑(在 '???' 标记之间)过滤日期:
WITH SET DateRange AS
[Dim Time].[Date].&[20170502]:[Dim Time].[Date].&[20170512]
MEMBER StudentsByDay AS
AGGREGATE(DateRange,
???CASE WHEN DateRange.CURRENTMEMBER
BETWEEN [Dim Students].[Start Date]
AND [Dim Students].[End Date]
THEN [Measures].[Students Count]
ELSE NULL END???)
SELECT
NON EMPTY { [StudentsByDay] } ON COLUMNS,
NON EMPTY { [DateRange] } ON ROWS
FROM [Education]
WHERE ( [Dim School].[School Code].&[123456] )
MDX 不像 SQL 那样灵活。我建议将以下条件放入您的 FactStudents table:
t.[date] between m.[StartDate] and m.[EndtDate]
并像单独的措施一样使用或替换现有的措施。
你需要以下东西:
- 事实 table 带有 DateID、StudentID 字段(参见下面的代码);
- 与Student和Date dims相关的度量组;
- 在 StudentID 字段上使用 DistinctCount 聚合的度量。
事实代码:
SELECT
t.[date] as DateID
,a.[SKStudent] as StudentID
FROM DM.DimTime t
CROSS JOIN DM.FactStudents a
INNER JOIN DM.DimStudents m ON a.SKStudent = m.SKStudent
INNER JOIN DM.DimSchool e ON a.SKSchool = e.SKSchool
WHERE e.SchoolCode = 123456
and t.[date] between m.[StartDate] and m.[EndtDate]
考虑到维度中的开始日期和结束日期,我需要在日期范围内计算按日期分组的度量。 objective是统计一段时间内某学校每天活跃的学生人数
以下 SQL 代码适用于数据仓库(多维数据集的来源)。
SELECT
t.[date]
,count(distinct a.SKStudent) as Students
FROM DM.DimTime t
CROSS JOIN DM.FactStudents a
INNER JOIN DM.DimStudents m ON a.SKStudent = m.SKStudent
INNER JOIN DM.DimSchool e ON a.SKSchool = e.SKSchool
WHERE t.[date] between '20170502' and '20170512'
and e.SchoolCode = 123456
and t.[date] between m.[StartDate] and m.[EndtDate]
GROUP BY t.[data]
结果集如下:
+--------------+----------+
| date | Students |
+--------------+----------+
| 2017-05-02 | 567 |
| 2017-05-03 | 567 |
| 2017-05-04 | 568 |
| 2017-05-05 | 570 |
| 2017-05-06 | 570 |
| 2017-05-07 | 570 |
| 2017-05-08 | 573 |
| 2017-05-09 | 573 |
| 2017-05-10 | 571 |
| 2017-05-11 | 568 |
| 2017-05-12 | 568 |
+--------------+----------+
我试着做了MDX代码。以下是我尝试过的。我不知道如何使用 CASE 语句中的逻辑(在 '???' 标记之间)过滤日期:
WITH SET DateRange AS
[Dim Time].[Date].&[20170502]:[Dim Time].[Date].&[20170512]
MEMBER StudentsByDay AS
AGGREGATE(DateRange,
???CASE WHEN DateRange.CURRENTMEMBER
BETWEEN [Dim Students].[Start Date]
AND [Dim Students].[End Date]
THEN [Measures].[Students Count]
ELSE NULL END???)
SELECT
NON EMPTY { [StudentsByDay] } ON COLUMNS,
NON EMPTY { [DateRange] } ON ROWS
FROM [Education]
WHERE ( [Dim School].[School Code].&[123456] )
MDX 不像 SQL 那样灵活。我建议将以下条件放入您的 FactStudents table:
t.[date] between m.[StartDate] and m.[EndtDate]
并像单独的措施一样使用或替换现有的措施。
你需要以下东西:
- 事实 table 带有 DateID、StudentID 字段(参见下面的代码);
- 与Student和Date dims相关的度量组;
- 在 StudentID 字段上使用 DistinctCount 聚合的度量。
事实代码:
SELECT
t.[date] as DateID
,a.[SKStudent] as StudentID
FROM DM.DimTime t
CROSS JOIN DM.FactStudents a
INNER JOIN DM.DimStudents m ON a.SKStudent = m.SKStudent
INNER JOIN DM.DimSchool e ON a.SKSchool = e.SKSchool
WHERE e.SchoolCode = 123456
and t.[date] between m.[StartDate] and m.[EndtDate]