MDX queries. How can to solve “IN” in “where” part

自古美人都是妖i 提交于 2019-12-12 02:08:18

问题


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


回答1:


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];



回答2:


This would be a classical case for Exists, slightly complicated as you have a many-to-many relationship between Menu Item Olds and Attributes:

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


来源:https://stackoverflow.com/questions/26869096/mdx-queries-how-can-to-solve-in-in-where-part

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!