在MDX中排序和限制结果

时间:2016-03-07 14:40:43

标签: mdx mondrian

我正在努力为每个" City"获得销量第一的前5名产品,按照" Sales"但我的代码不能正常工作。 查询查找前5个产品,但不是每个产品的前5个产品,ORDER DESC也不起作用。我用蒙德里安。有什么想法吗?

enter image description here

WITH
    SET [PRODUCTS] AS 
        Head(
            ORDER(Filter([Product].[Product].Members, not isEmpty([Measures].[Sales])),[Measures].[Sales], DESC)
        ,5
        )
SELECT
    NON EMPTY {[Measures].[Sales]} ON COLUMNS,
    NonEmptyCrossJoin([Markets].[City].Members, [PRODUCTS]) ON ROWS
FROM
    [SteelWheelsSales]

1 个答案:

答案 0 :(得分:2)

以下示例类似于针对AdvWrks的情况:

WITH 
  SET [AllCountries] AS [Country].[Country].MEMBERS 
  SET [AllStates]    AS [State-Province].[State-Province].MEMBERS 
  SET [Top2States] AS 
    Generate
    (
      [AllCountries]
     ,TopCount
      (
        (EXISTING 
          [AllStates])
       ,3
       ,[Measures].[Internet Order Count]
      )
    ) 
  MEMBER [State-Province].[All].[RestOfCountry] AS 
    Aggregate({(EXISTING {[AllStates]} - [Top2States])}) 
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,{
      [AllCountries]
    * 
      {
        [Top2States]
       ,[State-Province].[All].[RestOfCountry]
       ,[State-Province].[All]
      }
  } ON ROWS
FROM [Adventure Works];

它给出了这些结果:

enter image description here

因此,如果我尝试调整上述内容并对其进行简化,因为您不需要RestOf..类别,我会得到以下内容:

WITH 
  SET [AllCities]    AS [Markets].[City].MEMBERS
  //SET [AllPRODUCTS]  AS NONEMPTY([Product].[Product].MEMBERS, [Measures].[Sales])
  //alternative if NonEmpty not implemented in Mondrian..
  SET [AllPRODUCTS]  AS 
     FILTER(
         [Product].[Product].MEMBERS
        ,not isEmpty([Measures].[Sales])
     )
  SET [Top5PRODUCTS] AS 
    Generate
    (
      [AllCities]
     ,TopCount
      (
        (EXISTING 
          [AllPRODUCTS])
       ,5
       ,[Measures].[Sales]
      )
    ) 
SELECT 
  NON EMPTY 
    {[Measures].[Sales]} ON COLUMNS
 ,

    [AllCities] 
   *[Top5PRODUCTS]
   ON ROWS
FROM [SteelWheelsSales];