SQL Server 2008中SQL应用系列及BI笔记系列--目录索引
导读:本文介绍元数组(Tuples)的基础内容,已经了解的读者可以直接略过。
本文将包括以下内容:
■1、理解cube空间的概念。
■2、使用tuples从Cube中提取数据。
■3、使用多种语法引用层次结构成员(hierarchy members)。
本文所用数据库和所有源码,请到微软官网下载
1、N维空间(N-dimensional Space)和Cube空间
类似地,我们可以用(a1,a2,….,Ax)表示X维度的一个点。
2、使用Tuples访问数据
打开MDX查询编辑器窗口,输入
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar Year
]
.
[
All Periods
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
All Periods
]
,
[
Product
]
.
[
Category
]
.
[
Bikes
]
,
[
Product
]
.
[
Subcategory
]
.
[
Mountain Bikes
]
,
[
Measures
]
.
[
Measures
]
.
[
Reseller Sales Amount
]
)
;
/*
$26,492,684.38
*/
上述语句中,包含五部分元组(Tuples)
改变下元组(Tuples),结果相同。
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Product
]
.
[
Subcategory
]
.
[
Mountain Bikes
]
,
[
Date
]
.
[
Calendar Year
]
.
[
All Periods
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
All Periods
]
,
[
Product
]
.
[
Category
]
.
[
Bikes
]
,
[
Measures
]
.
[
Measures
]
.
[
Reseller Sales Amount
]
)
;
/*
$26,492,684.38
*/
4、理解Cells
每一个Cell包含了Value和FORMATTED_VALUE
可以修改默认值
关于Cell属性,请看MSDN(http://msdn.microsoft.com/zh-cn/library/ms145573.aspx)
5、使用局部元组(Partial Tuples)
当度量成员被移走时,一个局部元组(Partial Tuples)被提交,AS检查第一个默认的成员,提供缺失度量,这些度量的属性的层次结构的默认成员是Reseller Sales Amount,该成员将被应用,元组变完整。
6、使用用户层次结构生成Tuples
理解User-Hierarchy转换
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Calendar Year
]
.
[
CY 2003
]
.
[
FY 2003
]
;
/*
$12,000,247.33
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar Year
]
.
[
CY 2003
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
FY 2003
]
)
;
/*
$12,000,247.33
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Fiscal Year
]
.
[
FY 2003
]
;
/*
$15,921,423.19
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar Year
]
.
[
CY 2002
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
FY 2003
]
)
;
/*
$15,921,423.19
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Calendar Year
]
.
[
CY 2002
]
;
/*
$24,144,429.65
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
[
Date
]
.
[
Calendar Year
]
.
[
CY 2002
]
;
/*
$24,144,429.65
*/
避免引用冲突
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Calendar Year
]
.
[
CY 2002
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
FY 2003
]
)
;
/*
$15,921,423.19
*/
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Calendar Year
]
.
[
CY 2002
]
.
[
FY 2003
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
FY 2003
]
)
;
/*
$15,921,423.19
*/
而下面的引用是冲突的!
SELECT
FROM
[
Chapter 3 Cube
]
WHERE
(
[
Date
]
.
[
Calendar-To-Fiscal Year
]
.
[
Calendar Year
]
.
[
CY 2002
]
.
[
FY 2003
]
,
[
Date
]
.
[
Fiscal Year
]
.
[
FY 2002
]
)
;
/*
(null)
*/
本文对tuples有了一个初步的介绍,下文将关注MDX基础的另一个概念:结果集(Sets)。
参考资源:
1、MDX官方教程(http://msdn.microsoft.com/zh-cn/library/ms145506.aspx)