从Analysis Service

时间:2016-06-06 07:00:41

标签: sql mdx

我有这个问题:

SELECT 
  {
    [Measures].[ContractValue]
   ,[Measures].[ContractVolumeMWh]
   ,[Measures].[ContractVolumeMW]
  }
  DIMENSION PROPERTIES 
    PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME
   ON COLUMNS
 ,NON EMPTY 
    Hierarchize
    (
      DrillDownMember
      (
        DrillDownMember
        (
          DrillDownMember
          (
            DrillDownMember
            (
              CrossJoin
              (
                {
                  [HalfHourEnding].[Year].[All]
                 ,[HalfHourEnding].[Year].[Year].ALLMEMBERS
                }
               ,{
                  (
                    [HalfHourEnding].[Quarter].[All]
                   ,[HalfHourEnding].[Month].[All]
                   ,[Book].[Book].[All]
                   ,[InstrumentType].[InstrumentType].[All]
                  )
                }
              )
             ,[HalfHourEnding].[Year].[Year].ALLMEMBERS AS year
             ,[HalfHourEnding].[Quarter]
            )
           ,[HalfHourEnding].[Quarter].[Quarter].ALLMEMBERS
           ,[HalfHourEnding].[Month]
          )
         ,[HalfHourEnding].[Month].[Month].ALLMEMBERS
         ,[Book].[Book]
        )
       ,[Book].[Book].[Book].ALLMEMBERS
       ,[InstrumentType].[InstrumentType]
      )
    )
  DIMENSION PROPERTIES 
    PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME
   ON ROWS
FROM [ContractDetail]
WHERE 
  (
    [AsAt].[As at Business Date].&[2016-06-04T00:00:00]
   ,[Region].[Region].&[TTT]
  )
CELL PROPERTIES 
  VALUE
 ,FORMAT_STRING
 ,LANGUAGE
 ,BACK_COLOR
 ,FORE_COLOR
 ,FONT_FLAGS;

当我在Microsoft Analysis Server中运行时,我得到:

enter image description here

哪个是正确的输出,但是我没有得到五个第一列的标题。这会产生问题,因为当我想使用openquery从tsql中选择这些数据时,我无法选择任何这些列。

这是来自tsql的查询,它以正确的顺序提供了所有内容,但是我无法获得年度四分之一月份的bookType。

DECLARE @ContractsTable as TABLE ( 
                                                                                                         Year   varchar(100),
                                                                                                         Quarter   varchar(100),  
                Month  varchar(100),
                                                                                                         Book  varchar(100),      
             InstrumentType      varchar(100), 
                                                              Value                      decimal(16,2),
                                                                                                         Volume_MWH         decimal(16,2),  
                                                                                                         Volume_MW                decimal(16,2) 


                                                       )


Declare @ContractsQuery as nvarchar(max)
SET @ContractsQuery =       
'
SELECT 
"[Measures].[ContractValue]" as value, 
"[Measures].[ContractVolumeMWh]" as MWH,
"[Measures].[ContractVolumeMW]" as MW
from OpenQuery(CONTRACT_IMDB,     
   ''SELECT 
  {
    [Measures].[ContractValue]
   ,[Measures].[ContractVolumeMWh]
   ,[Measures].[ContractVolumeMW]
  }
  DIMENSION PROPERTIES 
    PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME
   ON COLUMNS
 ,NON EMPTY 
    Hierarchize
    (
      DrillDownMember
      (
        DrillDownMember
        (
          DrillDownMember
          (
            DrillDownMember
            (
              CrossJoin
              (
                {
                  [HalfHourEnding].[Year].[All]
                 ,[HalfHourEnding].[Year].[Year].ALLMEMBERS
                }
               ,{
                  (
                    [HalfHourEnding].[Quarter].[All]
                   ,[HalfHourEnding].[Month].[All]
                   ,[Book].[Book].[All]
                   ,[InstrumentType].[InstrumentType].[All]
                  )
                }
              )
             ,[HalfHourEnding].[Year].[Year].ALLMEMBERS AS year
             ,[HalfHourEnding].[Quarter]
            )
           ,[HalfHourEnding].[Quarter].[Quarter].ALLMEMBERS
           ,[HalfHourEnding].[Month]
          )
         ,[HalfHourEnding].[Month].[Month].ALLMEMBERS
         ,[Book].[Book]
        )
       ,[Book].[Book].[Book].ALLMEMBERS
       ,[InstrumentType].[InstrumentType]
      )
    )
  DIMENSION PROPERTIES 
    PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME
   ON ROWS
FROM [ContractDetail]
WHERE 
  (
    [AsAt].[As at Business Date].&[2016-06-04T00:00:00]
   ,[Region].[Region].&[TTT]
  )
CELL PROPERTIES 
  VALUE
 ,FORMAT_STRING
 ,LANGUAGE
 ,BACK_COLOR
 ,FORE_COLOR
 ,FONT_FLAGS; ''

)
' 


INSERT INTO @ContractsTable(Value,Volume_MWH,Volume_MW) 
EXEC sp_executesql @ContractsQuery 

Select * From @ContractsTable 

输出

enter image description here

现在我只是想从分析服务中提取输出,但我无法选择所有列。我无法弄清楚如何定义这些列,我在维度上向下钻取。

1 个答案:

答案 0 :(得分:0)

你可以像这样使用微信OPENQUERY

SELECT * FROM OPENQUERY(olap_server,
     'SELECT [unit sales] FROM myCubeName')

其中olap_server是链接的olap服务器的名称

您可以使用此处提供的execute olap函数: https://olapextensions.codeplex.com/

最初我们使用openquery但我们现在使用olapextensions,因为它看起来更简单,更有效。

修改

最初运行以下命令以查找所有可用列的标题:

Declare @ContractsQuery as nvarchar(max)
SET @ContractsQuery =       
'
SELECT 
*
from OpenQuery(CONTRACT_IMDB,     
   ''SELECT 
  {
    [Measures].[ContractValue]
   ,[Measures].[ContractVolumeMWh]
   ,[Measures].[ContractVolumeMW]
  }
  DIMENSION PROPERTIES 
    PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME
   ON COLUMNS
 ,NON EMPTY 
    Hierarchize
    (
      DrillDownMember
      (
        DrillDownMember
        (
          DrillDownMem
...
...

然后用特定名称替换*。