MDX月初至今年初查询

时间:2016-09-26 09:27:20

标签: sql-server date reporting-services ssas mdx

我正在尝试构建一个查询,该查询将从多维数据集中检索值,并根据当前日期构建一个月初至今和年初至今的SSRS报告。我基本上试图建立一个看起来像这样的报告......

Type Customer Product Group Quantity Shipped MTD Quantity Shipped YTD
=====================================================================

OE   CompanyX Bikes                    5                 396 
              Helmets                  10                254

     CompanyY Repair Kits              93                653
              T-shirts                 38                564
              Shoes                    10                120

我真的很挣扎到目前为止,这就是我想出来的......

 WITH 
 MEMBER [Measures].[Quantity Shipped YTD] As Sum(YTD([Date].[Dates].CurrentMember),[Measures].[Quantity Shipped])
 Member [Measures].[Quantity Shipped MTD] As Sum(MTD([Date].[Dates].CurrentMember),[Measures].[Quantity Shipped])
 SELECT NON EMPTY { [Measures].[Quantity Shipped MTD], [Measures].[Quantity Shipped YTD] } ON COLUMNS,
 NON EMPTY { ([Customer].[Business Type].[Business Type].ALLMEMBERS * [Customer].[Customer].[Customer].ALLMEMBERS * [Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS 
 FROM ( SELECT ( -{ [Customer Sales].[Summary Prod Group 1].&[Missing Product Group] } ) ON COLUMNS 
 FROM ( SELECT ( { [Customer].[Customer Code].&[2.254E3], [Customer].[Customer Code].&[2.063E3], [Customer].[Customer Code].&[2.183E3], [Customer].[Customer Code].&[2.261E3], [Customer].[Customer Code].&[5.3753E4], [Customer].[Customer Code].&[1.3084E5], [Customer].[Customer Code].&[1.37058E5] } ) ON COLUMNS 
 FROM ( SELECT ( { [Customer].[Business Type].&[OE] } ) ON COLUMNS 
 FROM ( SELECT ( [Date].[Day].&[Now()]) ON COLUMNS
 FROM [Sales])))) WHERE ( [Customer].[Customer Code].CurrentMember )  
 CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

上面的查询不会检索任何内容。我得到的只是一个空白的结果集。我需要提供更多的字段,但我假设如果我可以将数量运到工作中,我可以获得其他所有内容。请帮忙。 TX

2 个答案:

答案 0 :(得分:0)

目前,您没有任何日期in context,这意味着currentmember不会发现太多 - 要设置日期in context,您需要将实际日期成员添加到{ {1}}或WHERE子句 - 而不是SELECT的{​​{1}}子句。

SELECT是一个不寻常的名称,因为它是一个vba函数,但如果这个名字真的是,那么你可以把它移到where子句:

subselect

我怀疑这个Now()不是会员的名字而你正试图做一些动态的事情,在这种情况下你需要使用WITH MEMBER [Measures].[Quantity Shipped YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Quantity Shipped] ) MEMBER [Measures].[Quantity Shipped MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Quantity Shipped] ) SELECT NON EMPTY { [Measures].[Quantity Shipped MTD] ,[Measures].[Quantity Shipped YTD] } ON COLUMNS ,NON EMPTY { [Customer].[Business Type].[Business Type].ALLMEMBERS* [Customer].[Customer].[Customer].ALLMEMBERS* [Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS } ON ROWS FROM ( SELECT { [Customer].[Customer Code].&[2.254E3] ,[Customer].[Customer Code].&[2.063E3] ,[Customer].[Customer Code].&[2.183E3] ,[Customer].[Customer Code].&[2.261E3] ,[Customer].[Customer Code].&[5.3753E4] ,[Customer].[Customer Code].&[1.3084E5] ,[Customer].[Customer Code].&[1.37058E5] } ON 0 ,{[Customer].[Business Type].&[OE]} ON 1 ,- { [Customer Sales].[Summary Prod Group 1].&[Missing Product Group] } ON 2 FROM [Sales] ) WHERE [Date].[Day].&[Now()]; 进行研究

我在[Date].[Day].&[Now()];多维数据集中构建了类似的内容:

strToMember

它返回:

enter image description here

如果我想通过使用vba函数AdvWrks来实现上述动态,那么这需要发生:

WITH 
  MEMBER [Measures].[_YTD] AS 
    Aggregate
    (
      YTD([Date].[Calendar].CurrentMember)
     ,[Measures].[Order Quantity]
    ) 
  MEMBER [Measures].[_MTD] AS 
    Aggregate
    (
      MTD([Date].[Calendar].CurrentMember)
     ,[Measures].[Order Quantity]
    ) 
SELECT 
  {
    [Measures].[_YTD]
   ,[Measures].[_MTD]
  } ON COLUMNS
 ,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE 
  [Date].[Calendar].[Date].&[20050805];

答案 1 :(得分:0)

感谢为什么theq,这就是我想出来的......

WITH
  MEMBER [Measures].[Key for Today] AS 
    Format
    (
      Now()
     ,'yyyyMMdd'
    ) 
  MEMBER [Measures].[Today string] AS 
    '[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']' 

  MEMBER [Measures].[Quantity Shipped MTD] AS 
    Sum
    (
      MTD([Date].[Dates].CurrentMember)
     ,[Measures].[Quantity Shipped]
    ) 
  MEMBER [Measures].[Quantity Shipped YTD] AS 
    Sum
    (
      YTD([Date].[Dates].CurrentMember)
     ,[Measures].[Quantity Shipped]
    ) 

SELECT NON EMPTY
  {
    [Measures].[Quantity Shipped MTD],
    [Measures].[Quantity Shipped YTD]
  } ON COLUMNS, NON EMPTY
  {
    [Customer].[Business Type].[Business Type].ALLMEMBERS*
    [Customer].[Customer].[Customer].ALLMEMBERS*
    [Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS} On Rows
From
(
    SELECT 
    {
      [Customer].[Customer Code].&[2.254E3]
     ,[Customer].[Customer Code].&[2.063E3]
     ,[Customer].[Customer Code].&[2.183E3]
     ,[Customer].[Customer Code].&[2.261E3]
     ,[Customer].[Customer Code].&[5.3753E4]
     ,[Customer].[Customer Code].&[1.3084E5]
     ,[Customer].[Customer Code].&[1.37058E5]
    } ON 0
   ,{[Customer].[Business Type].&[OE]} ON 1
   ,-
      {
       [Customer Sales].[Summary Prod Group 1].&[Missing Product Group]
      } ON 2
From [Sales])
Where
StrToMember
    (
      [Measures].[Today string]
     ,constrained
    )