按日期

时间:2016-02-17 11:28:43

标签: sql-server ssas mdx olap

我想过滤查询结果,因此仅当日期大于指定时才会返回值。

我写了类似的东西:

SELECT { [Measures].[Net sales] } ON COLUMNS
    FROM [Sales]
    WHERE ( { [Department].[Department name].&[WRO], [Department].[Department name].&[KAT]},
            {( FILTER([Time].[Date], [Date].CURRENTMEMBER.MEMBER_KEY >= '2015-10-10'))} );

但确实返回null。如果此部分没有filter(),则会返回整个[Net sales]

2 个答案:

答案 0 :(得分:1)

也许是这样的:

WITH 
MEMBER [Measures].[Net sales NEW] AS 
   SUM(
     FILTER(
      [Time].[Date].[Date].MEMBERS, 
      [Time].[Date].CURRENTMEMBER.MEMBER_KEY 
          >= 20151010
     )
     , [Measures].[Net sales]
   )
SELECT 
      {
       [Measures].[Net sales]
      ,[Measures].[Net sales NEW] 
      } ON 0
FROM  [Sales]
WHERE {
       [Department].[Department name].&[WRO]
     , [Department].[Department name].&[KAT]
       };

另一种方法:

WITH 
MEMBER [Measures].[Date_key] AS 
      [Time].[Date].CURRENTMEMBER.MEMBER_KEY 
MEMBER [Measures].[Net sales NEW] AS 
   SUM(
      [Time].[Date].[Date].MEMBERS, 
      IIF(
         [Measures].[Date_key] >= 20151010
       , [Measures].[Net sales]
       , NULL
      )
   )
SELECT 
      {
       [Measures].[Net sales]
      ,[Measures].[Net sales NEW] 
      } ON 0
FROM  [Sales]
WHERE {
       [Department].[Department name].&[WRO]
     , [Department].[Department name].&[KAT]
       };

一个问题:这绝对是您的日期键的格式吗? “2015年10月10日

在WHERE切片器上检查密钥的一种可能的可视方式是使用更简单的脚本:

WITH 
MEMBER [Measures].[Date_key] AS 
      [Time].[Date].CURRENTMEMBER.MEMBER_KEY 
SELECT 
      [Measures].[Date_key] ON 0
      [Time].[Date].[Date].MEMBERS ON 1
FROM  [Sales]
WHERE {
       [Department].[Department name].&[WRO]
     , [Department].[Department name].&[KAT]
      };

不确定上述内容有什么问题 - 我明天会对AdvWrks立方体进行测试。

另一种方法可能是:

SELECT 
      [Measures].[Net sales] } ON 0
FROM  [Sales]
WHERE ( 
         { [Department].[Department name].&[WRO]
         , [Department].[Department name].&[KAT] }
         , {[Time].[Date].[Date].&[10 Oct 2015]:null} 
      );

注意:您需要将10 Oct 2015更改为多维数据集中的格式设置。 10 Oct 2015必须是多维数据集中存在的日期。

答案 1 :(得分:0)

试试这个:

SELECT { [Measures].[Net sales] } ON COLUMNS
FROM [Sales]
WHERE ( { [Department].[Department name].&[WRO], [Department].[Department name].&[KAT]},
       {( FILTER([Time].[Date], [Date].CURRENTMEMBER.MEMBER_KEY > '2015-10-10'))} );