SQL Server 2008中SQL应用系列及BI笔记系列--目录索引
导读:本文介绍集合(Sets)的进阶内容,本文将包括以下内容:
■1、Filter函数
■2、NonEmpty函数
■3、Set的Union、InterSection和Exception
■4、Generate和Extract函数
本文所用数据库和所有源码,请到微软官网下载
1、使用Filter函数(http://msdn.microsoft.com/zh-cn/library/ms146037.aspx)
例6-14
SELECT
{
([Measures].[Reseller Sales Amount]),
([Measures].[Internet Sales Amount])
} ON COLUMNS,
Filter(
{[Product].[Product].[Product].Members},
([Measures].[Internet Sales Amount]) > ([Measures].[Reseller Sales Amount])
) ON ROWS
FROM [Step-by-Step]
;
例6-15
SELECT
{
([Measures].[Reseller Sales Amount]),
([Measures].[Internet Sales Amount])
} ON COLUMNS,
Filter(
{[Product].[Product].[Product].Members},
([Measures].[Internet Sales Amount]) > ([Measures].[Reseller Sales Amount]) AND
(
[Product].[Category].CurrentMember Is [Product].[Category].[Clothing] OR
[Product].[Category].CurrentMember Is [Product].[Category].[Accessories]
)
) ON ROWS
FROM [Step-by-Step]
;
例6-16
SELECT
{
([Measures].[Reseller Sales Amount]),
([Measures].[Internet Sales Amount])
} ON COLUMNS,
Filter(
{[Product].[Product].[Product].Members},
([Measures].[Internet Sales Amount]) >
([Measures].[Reseller Sales Amount]) AND
NOT (
[Product].[Category].CurrentMember Is [Product].[Category].[Clothing] OR
[Product].[Category].CurrentMember Is [Product].[Category].[Accessories]
)
) ON ROWS
FROM [Step-by-Step]
;
2、使用NonEmpty函数(http://msdn.microsoft.com/en-us/library/ms145988.aspx)
例6-17
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
NonEmpty(
{[Product].[Product].[Product].Members},
{([Measures].[Reseller Sales Amount])}
) ON ROWS
FROM [Step-by-Step]
;
例6-18
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
NON EMPTY {[Product].[Product].[Product].Members} ON ROWS
FROM [Step-by-Step]
;
这两个例子中,使用NonEmpty函数和Non Empty关键字其实效果是一样的。
3、组合结果集
结果集的组合有三种形式:Union,intersection和Exception
如下图所示:

看原始例子:
例6-19:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2004])
) ON ROWS
FROM [Step-by-Step]
;

看看Union的效果:
例6-20:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Union(
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2003])
),
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2004])
)
) ON ROWS
FROM [Step-by-Step]
;

使用Exception的效果:
例6-21:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Except(
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2003])
),
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2004])
)
) ON ROWS
FROM [Step-by-Step]
;

InterSection的效果:
例6-22:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Intersect(
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2004])
),
TopCount(
{[Product].[Product].[Product].Members},
10,
([Measures].[Reseller Sales Amount], [Date].[Calendar Year].[CY 2003])
)
) ON ROWS
FROM [Step-by-Step]
;

4、Generate和Extract函数
MDX语言提供了两个强有力的“深奥”函数
Generate(http://msdn.microsoft.com/zh-cn/library/ms145526%28v=SQL.105%29.aspx)
Extract(http://msdn.microsoft.com/zh-cn/library/ms145980.aspx)
看原始语句:
例6-23:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
TopCount(
{[Product].[Product].[Product].Members},
5,
([Measures].[Reseller Sales Amount])
) ON ROWS
FROM [Step-by-Step]
;
加上Generate
例6-24:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Generate(
{[Product].[Category].[Category].Members},
TopCount(
{[Product].[Product].[Product].Members},
5,
([Measures].[Reseller Sales Amount])
)
) ON ROWS
FROM [Step-by-Step]
;
所得的结果与上例相同,

那么,generate起什么作用呢?我们再加上All标志符
例6-25:
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Generate(
{[Product].[Category].[Category].Members},
TopCount(
{[Product].[Product].[Product].Members},
5,
([Measures].[Reseller Sales Amount])
),
ALL
) ON ROWS
FROM [Step-by-Step]
;

换句话说,Generate其实是实现了遍历,按每个分类进行TopCount,这个在统计中非常棒。如果老板要统计今年每个月工作量最大的十个员工,那么你应该想到Generate,更进一步,你可以使用Category的hierarchy属性,得到如下结果:
例6-26
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Generate(
{[Product].[Category].[Category].Members},
{([Product].[Category].CurrentMember)} *
TopCount(
EXISTING {[Product].[Product].[Product].Members},
5,
([Measures].[Reseller Sales Amount])
),
ALL
) ON ROWS
FROM [Step-by-Step]
;
结果:

Extract的用途,先看下面的例子,使用Filter
例6-27
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Filter(
{[Product].[Product].[Product].Members} *
{[Date].[Calendar].[Month].Members},
([Measures].[Reseller Sales Amount])>160000
) ON ROWS
FROM [Step-by-Step]
;

使用Extract的例子
例6-28
SELECT
{([Measures].[Reseller Sales Amount])} ON COLUMNS,
Extract(
Filter(
{[Product].[Product].[Product].Members} *
{[Date].[Calendar].[Month].Members},
([Measures].[Reseller Sales Amount])>160000
),
[Product].[Product]
) ON ROWS
FROM [Step-by-Step]
;

请注意:Extract在此处相当于使用了distinct。
小结:本文是集合(Sets)的进阶,介绍了几个常用的函数如Filter和NonEmpty,Generate和Extract,集的组合方式如Union、InterSection和Exception。
下文将继续学习执行Aggregation的相关内容。
参考资源:
1、MDX官方教程(http://msdn.microsoft.com/zh-cn/library/ms145506.aspx)
分享到:
相关推荐
### Microsoft SQL Server 2008 MDX Step by Step (Feb 2009) #### 知识点一:Multidimensional Expressions (MDX) 基础 **Multidimensional Expressions (MDX)** 是一种用于查询多维数据集的语言。MDX 提供了一种...
《Microsoft SQL Server 2008 Analysis Services Step by Step》随书光盘包含了大量实践性的文件,旨在帮助读者深入理解并掌握SQL Server 2008的Analysis Services(简称SSAS)这一强大的数据仓库和商务智能工具。...
《Microsoft SQL Server 2008 MDX Step by Step》是一本专为SQL Server数据库管理员、数据分析师和开发人员设计的教程,旨在深入探讨多维表达式(Multidimensional Expressions,简称MDX)在SQL Server 2008中的应用...
Provides information on the fundamentals of Microsoft SQL Server 2005 Analysis Services. Teach yourself the fundamentals of SQL Server Analysis Services—one step at a time. With this practical, ...
《SQL Server 2008 Step by Step》是微软出版社发布的一本针对SQL Server 2008的全面教程,适合初学者和有一定基础的数据库管理员。这本书通过逐步指导的方式,帮助读者掌握SQL Server 2008的核心功能和操作技巧。 ...
《Microsoft SQL Server 2008 MDX 步步为营》是微软出版社于2009年2月推出的一本专门针对SQL Server 2008中的多维表达式(Multidimensional Expressions,简称MDX)技术的实战指南。这本书详细介绍了如何在SQL ...
Provides information on the fundamentals of Microsoft SQL Server 2005 Analysis Services. Teach yourself the fundamentals of SQL Server Analysis Services—one step at a time. With this practical, ...
电子书《Microsoft SQL Server 2008 Analysis Services Step by Step》很可能是逐步教程,涵盖了从安装配置到实际操作的整个过程。书中可能涉及以下内容: 1. **安装与配置**:如何在服务器上安装SQL Server 2008,...
This title serves as an authoritative guide to Microsofts new "SQL Server 2012 Analysis Services" BI product and is written by key members of the Microsoft Analysis Services product development team....
通过"MDX step by step"教程,读者将逐步学习如何构建这些查询,理解如何在多维环境中提取、分析和展示数据。书中每章的示例会帮助读者加深对MDX语法的理解,通过实际操作来探索不同场景下的应用。例如,可能有练习...
7. **工具支持**:了解如何在Microsoft Excel和SQL Server Management Studio(SSMS)中使用MDX,以及如何配合Power Pivot等工具进行更高级的数据分析。 提供的资源,如PPT、视频和文档,将为学习者提供丰富的交互...
《精通微软SQL Server 2008管理》 在IT领域,数据库管理是不可或缺的一环,而微软的SQL Server 2008作为一款广泛使用的数据库管理系统,深受企业和开发者的青睐。本文将深入探讨SQL Server 2008的核心管理知识,...
它起源于微软在1998年的SQL Server Analysis Services 7.0版本,现在已经成为一个标准化的语言,被其他OLAP(在线分析处理)供应商如Microstrategy Intelligence Server、Hyperion Essbase Server和SAS的Enterprise ...
SQL Server 2008是微软公司推出的一款关系型数据库管理系统,它在企业级数据管理、分析和报告方面表现出色。这款教程适用于初学者,旨在帮助用户掌握SQL Server 2008的基础知识和核心功能。 一、SQL Server 2008...
"Practical DMX Queries for Microsoft SQL Server Analysis Services 2008" contains more than 250 downloadable DMX queries you can use to extract and visualize data. The application, syntax, and results...
《Microsoft SQL Server 2012 Analysis Services 高级教程》是针对数据库管理员、数据分析师以及IT专业人士的一本深入指南,旨在帮助读者掌握SQL Server 2012中的Analysis Services(简称SSAS)的各项高级功能。...
Provides information on the fundamentals of Microsoft SQL Server 2005 Analysis Services. Teach yourself the fundamentals of SQL Server Analysis Services—one step at a time. With this practical, ...