每组的MDX TopPrice(最大值)

时间:2017-11-30 14:32:36

标签: group-by mdx

我有一个像

这样的查询
SELECT 
 NON EMPTY 
{
[Measures].[ItemPrice]
}
 ON COLUMNS , 
 NON EMPTY 
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
on rows

FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001],--Filtered for single item, just for simplicity. Will be all ItemKey s 
[DimDateView].[MonthName].[All]) 

输出

enter image description here

另一个查询

WITH 
  SET [AllItems] AS [DimItemView].[ItemName].[ItemName]
  SET [AllMonths] AS [DimDateView].[MonthOfYear].[MonthOfYear]

  SET [A] AS 
    Generate
    (
    {[AllItems]*[AllMonths] } AS s
     ,
       s.current*
        TopCount
        (
            [DimCustomerView].[CustomerName].[CustomerName]
            ,1
            ,[Measures].[ItemPrice]
        )
    )
SELECT 
  [Measures].[ItemPrice]
ON 0
 ,
[A] ON 1

FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001], --Filtered for single item, just for simplicity. Will be all ItemKey s 
[DimDateView].[MonthName].[All])

输出

enter image description here

我想要实现的是左连接2导致单个查询,但是对于相应组(ItemName和MonthOfYear)的左结果,每行包含TopPrice。期望的输出就像

enter image description here

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

如果您需要计算一次,我会使用以下代码:

WITH
MEMBER [Measures].[GroupPrice] AS
([DimCustomerView].[CustomerName].[All],[Measures].[ItemPrice])

SELECT 
NON EMPTY 
{
[Measures].[ItemPrice],
[Measures].[GroupPrice]
}
 ON COLUMNS , 
 NON EMPTY 
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
on rows

FROM [Model]
WHERE (
[DimCompanyView].[CompanyKey].&[smr],
[DimDateView].[Year].&[2017],
[DimCurrencyParam].[CurrencyCode].&[TL],
[DimItemView].[ItemKey].&[03001],--Filtered for single item, just for simplicity. Will be all ItemKey s 
[DimDateView].[MonthName].[All]) 

答案 1 :(得分:0)

ITEMPRICE = ExwAmount / QtyKg

实际上下面的代码给了我预期的结果,但是太慢了。任何改进?

WITH
MEMBER [Measures].[GroupPrice] AS
max([DimCustomerView].[CustomerName].Members,[Measures].[ItemPrice])

SELECT 
NON EMPTY 
{
[Measures].[ItemPrice],
[Measures].[GroupPrice]
}
 ON COLUMNS , 
 NON EMPTY 
[DimItemView].[ItemName].[ItemName]
*[DimDateView].[MonthOfYear].[MonthOfYear]
*[DimCustomerView].[CustomerName].[CustomerName]
having [Measures].[Fiili_Fiyat]<>0
on rows