简体   繁体   English

在 MDX 中使用 CASE 语句处理来自不同层次级别的数据

[英]Using CASE statement in MDX for data from different hierarchy level

I have the following hierarchy created:我创建了以下层次结构:

在此处输入图像描述

I have written this particular query to get my result:我写了这个特定的查询来得到我的结果:

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]);

The result that I am getting is:我得到的结果是:

在此处输入图像描述

This is not my desired result, for Ecotassa, I need to get the result for that particular year.这不是我想要的结果,对于 Ecotassa,我需要得到那一年的结果。 But I am getting null .但我得到null

Where am I going wrong with the query?我的查询哪里出错了?

This [Tempo].[Anno].CurrentMember is equal to All in your script hence it is choosing NULL in the CASE statement.[Tempo].[Anno].CurrentMember等于脚本中的All ,因此它在CASE语句中选择NULL I'd also be tempted to use [Tempo].[Periodo] for year and also for date: rather than [Tempo].[Anno] :我也很想使用[Tempo].[Periodo]作为年份和日期:而不是[Tempo].[Anno]

I've also thrown [Measures].[y] into the select so we can see if it is working ok.我还将[Measures].[y]放入 select 中,以便我们查看它是否工作正常。

So try this:所以试试这个:

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]
 );

test script测试脚本

What does this return?这会返回什么?

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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM