获取每个食品子类别的前5个品牌

时间:2019-03-30 14:58:03

标签: grouping mdx ranking

我想使用foodmart数据库将类别食品的每个子类别与前5个销售品牌相比,并将其与mdx查询中的年收入进行比较。

更具体地说,我们有一个名为product的维度,其中包含产品类别和产品的品牌名称。产品类别可能包含许多子类别。例如:

{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Blue Medal].[Blue Medal Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Giant].[Giant Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Jumbo].[Jumbo Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[National].[National Small Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Egg Substitute]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Large Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Large Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Small Brown Eggs]}
{[Product].[All Products].[Food].[Eggs].[Eggs].[Eggs].[Urban].[Urban Small Eggs]}

在上面的示例中,我们可以看到子类别鸡蛋包含许多子类别,其中也包含品牌。

最终目的是表明,对于子类别鸡蛋,年收入<10.000的人群中排名前5位的品牌是{list}。 我们希望使用度量单位销售额将其分类为食品的每个子类别。

输出结果应类似于:

salary   |Eggs                              |Meat
          Brand1|Brand2|Brand3|Brand4|Brand5|Brand1|Brand2|Brand3|Brand4|Brand5
< 10.000k |Name1 |Name2 |Name3 |Name4 |Name5 |Name1 |Name2 |Name3 |Name4 |Name5
> 10.000k |Name1 |Name2 |Name3 |Name4 |Name5 |Name1 |Name2 |Name3 |Name4 |Name5

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您需要根据排名过滤数据。看下面的例子。它基于冒险作品。在每个薪水组的示例中,我将根据其值返回每个产品类别的前三个子类别。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    public function testFunction()
    {
        return ("success!");
    }
}

结果

enter image description here

答案 1 :(得分:0)

这是您的脚本。有很多括号在向Mondrian发送元组信号:

WITH 
MEMBER [Measures].[Top5] AS 
RANK ( 
  (
   [Product].[Product Category].currentmember
  ,[Product].[Brand Name].CurrentMember
  )
  , ORDER( 
      (
         [Product].[Product Category].currentmember
        ,[Product].[Brand Name].[Brand Name].Members
      ) 
      , [Measures].[Store Sales]
      , BDESC
    ) 
  ) 
SELECT
non empty 
(
  [Product].[Product Category].[Product Category]
 ,filter(
    [Product].[Brand Name].[Brand Name]
  , [Measures].[Top5]<6
   )
) on columns, 
non empty (
   [Yearly_Income].[Yearly Income],
   [Measures].[Store Sales]
) on rows 
from [projetDW];

也许尝试使用CROSSJOIN函数来生成元组集:

WITH 
MEMBER [Measures].[Top5] AS 
RANK ( 
  CROSSJOIN(
   { [Product].[Product Category].currentmember }
  ,{ [Product].[Brand Name].CurrentMember }
  )
  , ORDER( 
      CROSSJOIN(
         { [Product].[Product Category].currentmember }
        ,[Product].[Brand Name].[Brand Name].Members
      ) 
      , [Measures].[Store Sales]
      , BDESC
    ) 
  ) 
SELECT
non empty 
CROSSJOIN(
  [Product].[Product Category].[Product Category].MEMBERS
 ,FILTER(
     [Product].[Brand Name].[Brand Name]
   , [Measures].[Top5]<6
  )
) on columns, 
non empty 
(
   [Yearly_Income].[Yearly Income],
   [Measures].[Store Sales]
) on rows 
from [projetDW];