MDX 查询原型

本篇文章记录 SBS 中 MDX 查询原型,可以根据这些查询原型来解决实际项目中的问题。

1. 查询在 2004年1月2日 - 2004年3月1日之间购买过 Bikes 产品的用户。

SELECT ([Product].[Category].[Bikes],[Measures].[Internet Sales Amount]) ON COLUMNS,
NON EMPTY [Customer].[Customer].[Customer].MEMBERS ON ROWS
FROM [Step-by-Step]
WHERE ([Date].[Calendar].[Date].&[]:[Date].[Calendar].[Date].&[])

2. 查询在 2004年1月2日 - 2004年3月1日之间购买过 Bikes 或 Clothing 产品的用户。

SELECT {
([Product].[Category].[Bikes],[Measures].[Internet Sales Amount]),
([Product].[Category].[Clothing],[Measures].[Internet Sales Amount])
}ON COLUMNS,
NON EMPTY [Customer].[Customer].[Customer].MEMBERS ON ROWS
FROM (
SELECT ([Date].[Calendar].[Date].&[]:[Date].[Calendar].[Date].&[]) ON COLUMNS
FROM [Step-by-Step]
)

3. 计算2004年3月1日 - 3月4日之前60天购买了 Bikes 或者 Clothing 产品的用户数量。

WITH
MEMBER [Measures].[CustomerCounts] AS
-- 计算用户总数
COUNT(
NONEMPTY (
-- 取购买过 Bikes 的用户 (当前时间-60天 至 当前时间)
{[Customer].[Customer].[Customer].MEMBERS},
{([Product].[Category].[Bikes],[Measures].[Internet Sales Amount])}
*LASTPERIODS(60,[Date].[Calendar].CurrentMember)
)
+ -- UNION 去掉重复的用户集合
NONEMPTY (
-- 取购买过 Clothing 的用户 (当前时间-60天 至 当前时间)
{[Customer].[Customer].[Customer].MEMBERS},
{([Product].[Category].[Clothing],[Measures].[Internet Sales Amount])}
*LASTPERIODS(60,[Date].[Calendar].CurrentMember)
)
)
-- 前60天的时间名称
MEMBER [Measures].[Pre60DateName] AS
LASTPERIODS(60,[Date].[Calendar].CurrentMember).Item(0).Name
SELECT {
[Measures].[CustomerCounts],
[Measures].[Pre60DateName]
} ON COLUMNS,
[Date].[Calendar].[Date].MEMBERS ON ROWS
FROM
(
-- 测试时间范围
SELECT ([Date].[Calendar].[Date].&[]:[Date].[Calendar].[Date].&[]) ON COLUMNS
FROM [Step-by-Step]
)

4.计算每个月第一天到前60天购买了 Bikes 或者 Clothing 产品的用户数量

WITH
MEMBER [Measures].[CustomerCounts] AS
-- 计算用户总数
COUNT(
NONEMPTY (
-- 取购买过 Bikes 的用户 (当前时间-60天 至 当前时间)
{[Customer].[Customer].[Customer].MEMBERS},
{([Product].[Category].[Bikes],[Measures].[Internet Sales Amount])}
*LASTPERIODS(60,[Date].[Calendar].CurrentMember.FIRSTCHILD)
)
+ -- UNION 去掉重复的用户集合
NONEMPTY (
-- 取购买过 Clothing 的用户 (当前时间-60天 至 当前时间)
{[Customer].[Customer].[Customer].MEMBERS},
{([Product].[Category].[Clothing],[Measures].[Internet Sales Amount])}
*LASTPERIODS(60,[Date].[Calendar].CurrentMember.FIRSTCHILD)
)
)
-- 前60天的时间名称
MEMBER [Measures].[Pre60DateName] AS
LASTPERIODS(60,[Date].[Calendar].CurrentMember.FIRSTCHILD).Item(0).Name
SELECT {
[Measures].[CustomerCounts],
[Measures].[Pre60DateName]
} ON COLUMNS,
[Date].[Calendar].[Month].MEMBERS ON ROWS
FROM
(
-- 测试时间范围
SELECT ([Date].[Calendar].[Date].&[]:[Date].[Calendar].[Date].&[]) ON COLUMNS
FROM [Step-by-Step]
)

更多 BI 文章请参看 BI 系列随笔列表 (SSIS, SSRS, SSAS, MDX, SQL Server)

如果觉得这篇文章看了对您有帮助,请帮助推荐,以方便他人在 BIWORK 博客推荐栏中快速看到这些文章。


上一篇:蓝桥杯 历届试题 大臣的旅费


下一篇:caffe的python接口学习(7):绘制loss和accuracy曲线