简体   繁体   中英

how to use Group by with top count in MDX query

I have an example where we prepared query in sql for fetching appropriate results

SQL Query-

select partnerid,BrandDesc,ActualRetailValue
from
(
select DENSE_RANK() over (partition by partnerid order by sum(ActualRetailValue) desc) as rnk,
partnerid,BrandDesc,sum(ActualRetailValue) as ActualRetailValue
from JDASales
where partnerid in (693,77)
group by partnerid,BrandDesc
) as A
where rnk <=5
order by partnerid,rnk

Output -

在此处输入图片说明

I want this result with mdx query.Even tryout with this code

SELECT 
  NON EMPTY 
    {[Measures].[Actual Retail Value]} ON COLUMNS
 ,NON EMPTY 
      [DimBrands].[Brand].[Brand].ALLMEMBERS
    * 
      TopCount
      (
          [DimPartners].[Partner].[Partner].ALLMEMBERS
        * 
          [DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
       ,5
       ,[Measures].[Actual Retail Value]
      ) ON ROWS
FROM 
(
  SELECT 
    {[DimPartners].[Partner].&[1275]} ON COLUMNS
  FROM 
  (
    SELECT 
      {[Dim Date].[Fiscal Year].&[2014-01-01T00:00:00]} ON COLUMNS
    FROM [SALES]
  )
)
WHERE 
  [Dim Date].[Fiscal Year].&[2014-01-01T00:00:00];

You can amend the rows snippet to use the GENERATE function:

SELECT 
  NON EMPTY 
    {[Measures].[Actual Retail Value]} ON 0
 ,NON EMPTY 
    GENERATE( 
     [DimBrands].[Brand].[Brand].ALLMEMBERS AS B
    , 
     TopCount(
          B.CURRENTMEMBER
         *[DimPartners].[Partner].[Partner].ALLMEMBERS
         *[DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
       ,5
       ,[Measures].[Actual Retail Value]
      )
    ) ON ROWS
...
...

This functions usage is detailed here: https://msdn.microsoft.com/en-us/library/ms145526.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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