如何从MDX查询中提取日期的月份

时间:2016-02-26 10:23:27

标签: sql reporting-services ssrs-2008 mdx

我需要使用日期过滤来自多维数据集的数据而不包含日期。 目前我正在使用以下MDX查询,即从一年的第一天开始并在最后一天结束时获取度量数据。

但是,我只想在行的日期时间中使用月份年,而不是在所有日期都使用DateTime。 (这里我的日期字段是:[期间]。[TransportDate])

以下是MDX查询。

SELECT 
  NON EMPTY 
    { 
      [Measures].[ConsignmentCount]
    , [Measures].[CBM]
    , [Measures].[LDM]
    , [Measures].[Weight]
    , [Measures].[Amount] } ON COLUMNS,
  NON EMPTY 
    { [Period].[TransportDate].Children } ON ROWS
FROM 
  ( 
     SELECT 
      ( 
        { [Period].[YearAndMonth].[ConsignmentYear].&[2014]
        , [Period].[YearAndMonth].[ConsignmentYear].&[2015] } 
      ) ON COLUMNS
      FROM [RebellOlap]
   )

以上查询从2015年第1天至2015年最后一天提取所有记录。见附图(所有记录)

enter image description here

但我想以某种方式(必需数据集) 我想要单列而不是月和年。所以 最终数据集应

(例如日期(07-2015),金额,CBM,LDM,Num,。托运)

enter image description here

我知道有一种方法可以从整个日期中提取月份和年份。但这只适用于单一日期。我想要包含所有日期必须使用包含月份和年份进行过滤,数据也应相应地对应于这些日期。参见上面预期的过滤数据集。

修改

WITH MEMBER [YearMo] AS 
     [Period].[ConsignmentYear].Currentmember.Name 
     + "-" + 
     [Period].[ConsignmentMonth].Currentmember.Name
SELECT 
  NON EMPTY 
    { 
      [Measures].[ConsignmentCount]
    , [Measures].[CBM]
    , [Measures].[LDM]
    , [Measures].[Weight]
    , [Measures].[Amount] 
   } ON COLUMNS , 
NON EMPTY 
   (
     {  
       [Period].[YearAndMonth].[ConsignmentMonth] 
     } 
   ) ON ROWS 
FROM 
  ( 
     SELECT 
        ( 
          { 
            [Period].[YearAndMonth].[ConsignmentYear].&[2014]
          , [Period].[YearAndMonth].[ConsignmentYear].&[2015] 
          } 
        ) ON COLUMNS
     FROM [RebellOlap]
  )
上面的

会产生结果但没有年份。

enter image description here

我想以某种方式在同一列上有月份和月份。 所以01将成为01-2014。 你能帮我吗

没有连接的结果

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须在维度视图(cube design-sql view)中创建一个新字段为'YYYY-mm',或者您可以连接年份和&月维属性(如下例所示)。

示例代码:

WITH MEMBER [YearMo] AS 
   [Period].[Monthly].Currentmember.Name 
    + "-" + [Period].[YearAndMonth].Currentmember.Name 
SELECT 
  NON EMPTY {[Measures].[ConsignmentCount], [Measures].[CBM], [Measures].[LDM], [Measures].[Weight], [Measures].[Amount]} ON COLUMNS 
, NON EMPTY ( { [Period].[Monthly].[Year] } , { [Period].[YearAndMonth].[ConsignmentMonth] } ) ON ROWS 
FROM 
  ( 
    SELECT ( { [Period].[YearAndMonth].[ConsignmentYear].&[2014], [Period].[YearAndMonth].[ConsignmentYear].&[2015] } ) ON COLUMNS 
    FROM [RebellOlap]
   ) 

答案 1 :(得分:0)

也许尝试在不同的层次结构中托管计算成员。我已经猜到了这个[Forwarder].[Forwarder].[All],您需要调整到您的多维数据集中存在的层次结构:

WITH MEMBER [Forwarder].[Forwarder].[All].[YearMo] AS 
     [Period].[Year].Currentmember.Name 
     + "-" + 
     [Period].[Month].Currentmember.Name
SELECT 
  NON EMPTY 
    { 
      [Measures].[ConsignmentCount]
    , [Measures].[CBM]
    , [Measures].[LDM]
    , [Measures].[Weight]
    , [Measures].[Amount] 
   } ON COLUMNS , 
NON EMPTY 
      [Period].[Year].[Year]
     *[Period].[Month].[Month]
     *[Forwarder].[Forwarder].[All].[YearMo] 
     ON ROWS 
FROM 
  ( 
     SELECT 
        ( 
          { 
            [Period].[YearAndMonth].[ConsignmentYear].&[2014]
          , [Period].[YearAndMonth].[ConsignmentYear].&[2015] 
          } 
        ) ON COLUMNS
     FROM [RebellOlap]
  )

如果以上产生错误,那么您可能需要先创建一个度量:

WITH 
MEMBER [Measures].[YearMoString] AS 
     [Period].[Year].Currentmember.Name 
     + "-" + 
     [Period].[Month].Currentmember.Name
MEMBER [Forwarder].[Forwarder].[All].[YearMo] AS 
     (
       [Forwarder].[Forwarder].[All]
      ,[Measures].[YearMoString]
     )
SELECT 
  NON EMPTY 
    { 
      [Measures].[ConsignmentCount]
    , [Measures].[CBM]
    , [Measures].[LDM]
    , [Measures].[Weight]
    , [Measures].[Amount] 
   } ON COLUMNS , 
NON EMPTY 
      [Period].[Year].[Year]
     *[Period].[Month].[Month]
     *[Forwarder].[Forwarder].[All].[YearMo] 
     ON ROWS 
FROM 
  ( 
     SELECT 
        ( 
          { 
            [Period].[YearAndMonth].[ConsignmentYear].&[2014]
          , [Period].[YearAndMonth].[ConsignmentYear].&[2015] 
          } 
        ) ON COLUMNS
     FROM [RebellOlap]
  )