Search code examples
where-clausemdxolap

MDX queries. How can to solve "IN" in "where" part


I have sql query and i need this query execute in olap cube .

select count(distinct mi.id) from  [MTD_DEV].[dbo].[MenuItemAttributes] as m
  inner join  [dbo].[MenuItemOlds] as  mi
  on mi.id = m.MenuItemId
  inner join [dbo].[RestaurantlistItems] as rl
  on rl.RestaurantId = mi.RestaurantId
  where  m.AttributeId = 31 and rl.RestaurantListId = 69 and mi.PeriodId = 99 and m.MenuItemId in (select MenuItemId from  [MTD_DEV].[dbo].[MenuItemAttributes] where AttributeId = 6

and i have working mdx query and I need to add operator 'IN' or something another solution for this query

  SELECT CROSSJOIN( 
    {[Measures].[Menu Item Olds Count],[Measures].[Restaurantlist Items Count]},
    {[Periods].[Id].[99],[Periods].[Id].[93],[Periods].[Id].[75]}) ON COLUMNS,
    {[Menu Item Olds].[id]} ON ROWS
     FROM [MTD DEV]
     where (
     {[Restaurant Lists].[Id].[69]},
     {[Attributes].[Id].[6]} ,
     {[Attribute Categories].[Id].[5]} -- or can use the same parameter {[Attributes].[Id].[31]}
           )

for better understanding: https://drive.google.com/file/d/0B3rw0YPItJIIa3FfNEtrVC04SVU/view?usp=sharing

Additional Comments to question

In ms sql I have to slice MenuItemOlds by some parameter m.AttributeId = 31 annd then from result I have to slice again for parameter AttributeId = 6. In Sql it looks like this:

select count(distinct mi.id) from  [MTD_DEV].[dbo].[MenuItemAttributes] as m
  inner join  [dbo].[MenuItemOlds] as  mi on mi.id = m.MenuItemId
  where  m.AttributeId = 31 and m.MenuItemId in (select MenuItemId from  [MTD_DEV].[dbo].[MenuItemAttributes] where AttributeId = 6

I have problem in OLAP Cube. How I see to solve this problem :

1.I get all data where AttributeId = 31

 SELECT CROSSJOIN( 
    {[Measures].[Menu Item Olds Count],[Measures].[Restaurantlist Items Count]},
    {[Periods].[Id].[99],[Periods].[Id].[93],[Periods].[Id].[75]}) ON COLUMNS,
    {[Menu Item Olds].[id]} ON ROWS
     FROM [MTD DEV]
     where ({[Attributes].[Id].[31]})

the result of this - all catering menu items

  1. After this , in this collection of Menu Items, I need to find all menu items where {[Attributes].[Id].[6]} (kids menu)

When i am trying to execute such query :

 SELECT CROSSJOIN( 
    {[Measures].[Menu Item Olds Count],[Measures].[Restaurantlist Items Count]},
    {[Periods].[Id].[99],[Periods].[Id].[93],[Periods].[Id].[75]}) ON COLUMNS,
    {[Menu Item Olds].[id]} ON ROWS
     FROM [MTD DEV]
     where (
     {[Attributes].[Id].[6]} ,
     {[Attributes].[Id].[31]}
           )

I get result , in which i have Menu items with AttributeId.[6] + menuItem with attributeId.[31] for example:


count of menu items with AttributeId.[6] = 11000 items


count of menu items with AttributeId.[31] = 724000items


and result is 724000+11000 = 735000 but i don`t need it


i need to find all items with AttributeId.[31], and in this collection i need to find items with AttributeId.[6] The right result of query must be less than 11000 items


Solution

  • Does NonEmpty with intersect work as an alternative?

    SELECT 
        {
          [Measures].[Menu Item Olds Count]
         ,[Measures].[Restaurantlist Items Count]
        }
      * 
        {
          [Periods].[Id].[99]
         ,[Periods].[Id].[93]
         ,[Periods].[Id].[75]
        } ON COLUMNS
     ,Intersect
      (
        NonEmpty
        (
          [Menu Item Olds].[id].[id].MEMBERS
         ,(
            [Attributes].[Id].[31]
           ,{
              [Measures].[Menu Item Olds Count]
             ,[Measures].[Restaurantlist Items Count]
            }
          )
        )
       ,NonEmpty
        (
          [Menu Item Olds].[id].[id].MEMBERS
         ,(
            [Attributes].[Id].[6]
           ,{
              [Measures].[Menu Item Olds Count]
             ,[Measures].[Restaurantlist Items Count]
            }
          )
        )
      ) ON ROWS
    FROM [MTD DEV];