在MDX中使用CASE语句来获取来自不同层次结构级别的数据

时间:2016-06-14 07:32:16

标签: case mdx

我创建了以下层次结构:

enter image description here

我写了这个特定的查询来得到我的结果:

WITH MEMBER [Ecotassa] AS 
CASE 
   WHEN [Tempo].[Anno].CurrentMember.MemberValue = '2011' THEN '9114646'
   WHEN [Tempo].[Anno].CurrentMember.MemberValue = '2012' THEN '8787551.65'
   WHEN [Tempo].[Anno].CurrentMember.MemberValue = '2013' THEN '8435651.26'
   WHEN [Tempo].[Anno].CurrentMember.MemberValue = '2014' THEN '8917336.39'
   WHEN [Tempo].[Anno].CurrentMember.MemberValue = '2015' THEN '9465533.37'
   ELSE  NULL
END

SELECT 
{ [Measures].[Dato], [Ecotassa] } ON COLUMNS,
{ ([Tempo].[Periodo].[Mese Anno].MEMBERS) } ON ROWS

FROM [AGRO]
WHERE ([TipoMisura].[Tipo Misura].&[1], [TipoAnno].[Tipo Anno].&[2],
[TipoPeriodo].[Tipo Periodo].&[2],
[Mercato].[Mercato].&[1],
[Aziende].[Descrizione Codice Azienda].&[100 - BASF]);

我得到的结果是:

enter image description here

这不是我想要的结果,对于Ecotassa,我需要得到特定年份的结果。但我得到 null

不知道我的查询出错了。有人可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:1)

[Tempo].[Anno].CurrentMember在您的脚本中等于All,因此它会在NULL语句中选择CASE。我也很想在年份和日期使用[Tempo].[Periodo]:而不是[Tempo].[Anno]

我还将[Measures].[y]抛入选择中,以便我们可以看到它是否正常工作。

所以试试这个:

WITH 
MEMBER [Measures].[y] AS 
  EXISTS(
    [Tempo].[Periodo].[Anno].members,
    [Tempo].[Periodo].CurrentMember
  ).ITEM(0).ITEM(0).Member_Caption
MEMBER [Measures].[Ecotassa] AS 
  CASE 
     WHEN [Measures].[y] = '2011' THEN '9114646'
     WHEN [Measures].[y] = '2012' THEN '8787551.65'
     WHEN [Measures].[y] = '2013' THEN '8435651.26'
     WHEN [Measures].[y] = '2014' THEN '8917336.39'
     WHEN [Measures].[y] = '2015' THEN '9465533.37'
   ELSE  NULL
  END
SELECT 
{ 
   [Measures].[Dato]
  ,[Measures].[Ecotassa]
  ,[Measures].[y] 
 } ON COLUMNS,
[Tempo].[Periodo].[Mese Anno].MEMBERS ON ROWS
FROM [AGRO]
WHERE (
   [TipoMisura].[Tipo Misura].&[1]
  ,[TipoAnno].[Tipo Anno].&[2]
  ,[TipoPeriodo].[Tipo Periodo].&[2]
  ,[Mercato].[Mercato].&[1]
  ,[Aziende].[Descrizione Codice Azienda].&[100 - BASF]
 );

测试脚本

这又是什么回报?

WITH 
MEMBER [Measures].[EcotassaTEST] AS 
  [Tempo].[Anno].CurrentMember.MemberValue

SELECT 
{ [Measures].[Dato], [EcotassaTEST] } ON COLUMNS,
{ ([Tempo].[Periodo].[Mese Anno].MEMBERS) } ON ROWS

FROM [AGRO]
WHERE ([TipoMisura].[Tipo Misura].&[1], [TipoAnno].[Tipo Anno].&[2],
[TipoPeriodo].[Tipo Periodo].&[2],
[Mercato].[Mercato].&[1],
[Aziende].[Descrizione Codice Azienda].&[100 - BASF]);