在MDX查询上添加过滤器

时间:2015-02-02 17:41:57

标签: sql mdx olap

SELECT  
  {[Measures].[Imports - Tons]
   , [Measures].[Imports - Metric Tonnes]
   , [Measures].[Import Customs Value]  } ON COLUMNS,  
{CROSSJOIN (
   [Date].[Month Year].members
  ,[Country].[Country Name].members
  ,[Steel Mill Product].[Steel Mill Product Name].[Plates-Cut Length]
  ,[Concordance].[HTS 10 Digit Code].members) }
ON ROWS  
FROM [AISISteel]   
WHERE ([Date].[Year].[2014], [Concordance].[HTS 2 Digit Code].[72])

我有这个MDX查询 我想要取回所有这些数据,但只返回Import-Tons> 0的数据。我该怎么做?

1 个答案:

答案 0 :(得分:5)

您可以尝试使用FILTER功能:

SELECT  
  {[Measures].[Imports - Tons]
   , [Measures].[Imports - Metric Tonnes]
   , [Measures].[Import Customs Value]  } ON COLUMNS,  
FILTER(
  {CROSSJOIN (
     [Date].[Month Year].members
    ,[Country].[Country Name].members
    ,[Steel Mill Product].[Steel Mill Product Name].[Plates-Cut Length]
    ,[Concordance].[HTS 10 Digit Code].members) }
  , [Measures].[Imports - Tons] > 0
  )
ON ROWS  
FROM [AISISteel]   
WHERE ([Date].[Year].[2014], [Concordance].[HTS 2 Digit Code].[72])

此外,很少使用的HAVING子句是您的可能选项

SELECT  
  {[Measures].[Imports - Tons]
   , [Measures].[Imports - Metric Tonnes]
   , [Measures].[Import Customs Value]  } 
ON COLUMNS,  
  {CROSSJOIN (
     [Date].[Month Year].members
    ,[Country].[Country Name].members
    ,[Steel Mill Product].[Steel Mill Product Name].[Plates-Cut Length]
    ,[Concordance].[HTS 10 Digit Code].members) }
HAVING [Measures].[Imports - Tons] > 0
ON ROWS  
FROM [AISISteel]   
WHERE ([Date].[Year].[2014], [Concordance].[HTS 2 Digit Code].[72])

同样只是为了简洁,CROSSJOIN功能有一个快捷方式,几乎总是首选:

SELECT  
  {[Measures].[Imports - Tons]
   , [Measures].[Imports - Metric Tonnes]
   , [Measures].[Import Customs Value]  } 
ON COLUMNS,  
    {[Date].[Month Year].members*
     [Country].[Country Name].members*
     [Steel Mill Product].[Steel Mill Product Name].[Plates-Cut Length]*
     [Concordance].[HTS 10 Digit Code].members}
HAVING [Measures].[Imports - Tons] > 0
ON ROWS  
FROM [AISISteel]   
WHERE ([Date].[Year].[2014], [Concordance].[HTS 2 Digit Code].[72])

修改

如果将上述内容与以下内容进行比较,执行时间会有很大差异吗?

SELECT  
  {[Measures].[Imports - Tons]
   , [Measures].[Imports - Metric Tonnes]
   , [Measures].[Import Customs Value]  } ON COLUMNS,  
  FILTER(
    NONEMPTY(
     [Date].[Month Year].members*
     [Country].[Country Name].members*
     [Steel Mill Product].[Steel Mill Product Name].[Plates-Cut Length]*
     [Concordance].[HTS 10 Digit Code].members
    ,[Measures].[Imports - Tons]
    )
  ,[Measures].[Imports - Tons] > 0
  )
ON ROWS  
FROM [AISISteel]   
WHERE ([Date].[Year].[2014], [Concordance].[HTS 2 Digit Code].[72])