SQL Server 2008 MDX学习笔记之结果集Sets使用技巧

数据库 SQL Server
本文我们主要介绍了SQL Server 2008 MDX学习笔记之结果集Sets使用技巧,是通过具体的实例来介绍的,希望本次的介绍能够对您有所帮助。

SQL Server 2008 MDX学习笔记之结果集Sets使用技巧是本文我们主要要介绍的内容,接下来就让我们从以下的几个例子中区了解结果集Sets的使用技巧吧。

Sets的基本技巧

在Analysis Service中,Set代表元组(Tuples)的集合。在一个Set内部,独立的元组被用逗号隔开,如下:

  1. {   
  2. ([Product].[Category].[Accessories]),   
  3. ([Product].[Category].[Bikes]),   
  4. ([Product].[Category].[Clothing]),   
  5. ([Product].[Category].[Components])   

下面我们组装一个基本的Sets

SQL Server 2008 MDX学习笔记之结果集Sets使用技巧

打开MDX查询编辑器,如下:

例4-1

  1. SELECT  
  2. {  
  3. ([Date].[Calendar].[CY 2002], [Geography].[Country].[United States]),  
  4. ([Date].[Calendar].[CY 2003], [Geography].[Country].[United States]),  
  5. ([Date].[Calendar].[CY 2004], [Geography].[Country].[United States])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Category].[Accessories]),  
  9. ([Product].[Category].[Bikes]),  
  10. ([Product].[Category].[Clothing]),  
  11. ([Product].[Category].[Components])  
  12. } ON ROWS  
  13. FROM [Step-by-Step]; 

我们增加一个元组([Product].[Subcategory].[Mountain Bikes]),如下:

例4-2

  1. SELECT  
  2. {  
  3. ([Date].[Calendar].[CY 2002], [Geography].[Country].[United States]),  
  4. ([Date].[Calendar].[CY 2003], [Geography].[Country].[United States]),  
  5. ([Date].[Calendar].[CY 2004], [Geography].[Country].[United States])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Category].[Accessories]),  
  9. ([Product].[Category].[Bikes]),  
  10. ([Product].[Category].[Clothing]),  
  11. ([Product].[Category].[Components]),  
  12. ([Product].[Subcategory].[Mountain Bikes])  
  13. } ON ROWS  
  14. FROM [Step-by-Step];  
  15. /*  
  16. Executing the query ...  
  17. Members belong to different hierarchies in the function.  
  18. 函数中指定的两个集具有不同的维数。  
  19. Execution complete  
  20. */ 

我们使用相同维数的用户层次结构[Product Categories],修改如下:

例4-3

  1. SELECT  
  2. {  
  3. ([Date].[Calendar].[CY 2002], [Geography].[Country].[United States]),  
  4. ([Date].[Calendar].[CY 2003], [Geography].[Country].[United States]),  
  5. ([Date].[Calendar].[CY 2004], [Geography].[Country].[United States])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Product Categories].[Accessories]),  
  9. ([Product].[Product Categories].[Bikes]),  
  10. ([Product].[Product Categories].[Clothing]),  
  11. ([Product].[Product Categories].[Components]),  
  12. ([Product].[Product Categories].[Mountain Bikes])  
  13. } ON ROWS  
  14. FROM [Step-by-Step];  
  15. /* CY 2002 CY 2003 CY 2004  
  16. United States United States United States  
  17. Accessories $61,263.90 $151,136.35 $76,027.18  
  18. Bikes $14,716,804.14 $16,139,984.68 $7,951,335.55  
  19. Clothing $317,939.41 $495,443.62 $197,590.92  
  20. Components $2,526,542.06 $3,284,551.84 $1,137,105.72  
  21. Mountain Bikes $6,970,418.73 $5,832,626.02 $2,539,198.92  
  22. */ 

SQL Server 2008 MDX学习笔记之结果集Sets使用技巧

下面这个查询有类似错误:

例4-4

  1. SELECT  
  2. {  
  3. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2002]),  
  4. ([Date].[Calendar].[CY 2003], [Geography].[Country].[United States]),  
  5. ([Date].[Calendar].[CY 2004], [Geography].[Country].[United States])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Product Categories].[Accessories]),  
  9. ([Product].[Product Categories].[Bikes]),  
  10. ([Product].[Product Categories].[Clothing]),  
  11. ([Product].[Product Categories].[Components]),  
  12. ([Product].[Product Categories].[Mountain Bikes])  
  13. } ON ROWS  
  14. FROM [Step-by-Step];  
  15. /*  
  16. Executing the query ...  
  17. Query (2, 4) Two sets specified in the function have different dimensionality.  
  18. 函数中指定的两个集具有不同的维数。  
  19. Execution complete  
  20. */ 

正确应为:

例4-5

  1. SELECT  
  2. {  
  3. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2002]),  
  4. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2003]),  
  5. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2004])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Product Categories].[Accessories]),  
  9. ([Product].[Product Categories].[Bikes]),  
  10. ([Product].[Product Categories].[Clothing]),  
  11. ([Product].[Product Categories].[Components]),  
  12. ([Product].[Product Categories].[Mountain Bikes])  
  13. } ON ROWS  
  14. FROM [Step-by-Step]; 

我们可以这样改变顺序,并增加一个行:

例4-6

  1. SELECT  
  2. {  
  3. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2004]),  
  4. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2003]),  
  5. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2002])  
  6. } ON COLUMNS,  
  7. {  
  8. ([Product].[Product Categories].[Accessories]),  
  9. ([Product].[Product Categories].[Accessories]),  
  10. ([Product].[Product Categories].[Bikes]),  
  11. ([Product].[Product Categories].[Clothing]),  
  12. ([Product].[Product Categories].[Components]),  
  13. ([Product].[Product Categories].[Mountain Bikes])  
  14. } ON ROWS  
  15. FROM [Step-by-Step]; 

查询结果如下:

  1. /* United States United States United States  
  2. CY 2004 CY 2003 CY 2002  
  3. Accessories $76,027.18 $151,136.35 $61,263.90  
  4. Accessories $76,027.18 $151,136.35 $61,263.90  
  5. Bikes $7,951,335.55 $16,139,984.68 $14,716,804.14  
  6. Clothing $197,590.92 $495,443.62 $317,939.41  
  7. Components $1,137,105.72 $3,284,551.84 $2,526,542.06  
  8. Mountain Bikes $2,539,198.92 $5,832,626.02 $6,970,418.73  
  9. */ 

多出的重复行怎么办?我们可以使用distinct函数(http://msdn.microsoft.com/zh-cn/library/ms146033.aspx)

例4-6

  1. SELECT  
  2. {  
  3. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2004]),  
  4. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2003]),  
  5. ([Geography].[Country].[United States], [Date].[Calendar].[CY 2002])  
  6. } ON COLUMNS,  
  7. DISTINCT(  
  8. {  
  9. ([Product].[Product Categories].[Accessories]),  
  10. ([Product].[Product Categories].[Accessories]),  
  11. ([Product].[Product Categories].[Bikes]),  
  12. ([Product].[Product Categories].[Clothing]),  
  13. ([Product].[Product Categories].[Components]),  
  14. ([Product].[Product Categories].[Mountain Bikes])  
  15. }  
  16. ) ON ROWS  
  17. FROM [Step-by-Step];  
  18. /* United States United States United States  
  19. CY 2004 CY 2003 CY 2002  
  20. Accessories $76,027.18 $151,136.35 $61,263.90  
  21. Bikes $7,951,335.55 $16,139,984.68 $14,716,804.14  
  22. Clothing $197,590.92 $495,443.62 $317,939.41  
  23. Components $1,137,105.72 $3,284,551.84 $2,526,542.06  
  24. Mountain Bikes $2,539,198.92 $5,832,626.02 $6,970,418.73  
  25. */ 

关于SQL Server 2008 MDX学习笔记之结果集Sets使用技巧的知识就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

  1. SQL Server数据库DataRelation的应用示例详解
  2. SQL Server 2008 MDX应用之检索集合中的元组
  3. SQL Server 2005/2008中的CTE应用之递归查询
  4. SQL Server 2008数据库学习笔记之MDX查询示例
  5. 浅析SQL Server数据库SSIS导入IIS日志的简单步骤


 

责任编辑:赵鹏 来源: 博客园
相关推荐

2011-08-29 11:38:18

SQL Server 联合结果集union

2011-08-19 15:13:36

SQL Server MDX查询

2011-08-19 15:29:10

SQL Server 元数组

2011-08-19 13:46:22

SQL Server 组装有序集合

2011-08-19 14:03:36

SQL Server 检索集合

2011-08-11 14:23:57

SQL Server 索引分区

2011-08-29 13:10:59

SQL Server 给新加字段赋值

2012-04-06 10:13:08

SQLSQL Server

2011-08-22 11:23:41

SQL Server 数据修改

2011-07-25 14:28:38

SQL Server数多维数据集

2009-04-16 16:54:53

集成IntegrationSSIS

2011-08-29 15:52:19

SQL ServerMybatis存储过程

2011-04-11 12:55:34

SQL Server 平面文件

2011-08-19 10:40:27

SQL Server Merge命令

2010-06-28 08:37:58

SQL Server

2011-08-19 10:13:34

SQL Server Values新用途

2011-08-19 11:00:54

SQL Server WaitFor命令

2011-08-19 10:24:46

SQL Server Top新用途

2011-08-16 09:48:27

SQLPLUS学习笔记SQL Buffer

2010-08-31 16:35:59

CSS
点赞
收藏

51CTO技术栈公众号