MDX查询。如何在“where”部分解决“IN”

时间:2014-11-11 16:02:27

标签: where mdx olap

我有sql查询,我需要在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

我正在使用mdx查询和我需要为此查询添加运算符' IN' 或其他解决方案

  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]}
           )
     

为了更好地理解:   https://drive.google.com/file/d/0B3rw0YPItJIIa3FfNEtrVC04SVU/view?usp=sharing

对问题的补充评论 n

在ms sql中,我必须通过某个参数m.AttributeId = 31对MenuItemOlds进行切片 然后从结果我必须再次切片参数AttributeId = 6。 在Sql中它看起来像这样:

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

我在OLAP Cube中遇到问题。 我如何看待解决这个问题:

1.我得到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]})

结果 - 所有餐饮菜单项

  1. 在此之后,在这个菜单项集合中,我需要找到所有菜单项{{Attributes]。[Id]。[6]}(儿童菜单)
  2. 当我尝试执行此类查询时:

     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]}
               )
    

    我得到了结果,其中我有带AttributeId的菜单项。[6] + menuItem with attributeId。[31] 例如:


    具有AttributeId的菜单项数。[6] = 11000件


    具有AttributeId的菜单项数。[31] = 724000items


    ,结果是724000 + 11000 = 735000,但我不需要它


    我需要使用AttributeId找到所有项目。[31],在这个集合中我需要找到具有AttributeId的项目。[6] 查询的正确结果必须少于11000项

2 个答案:

答案 0 :(得分:0)

这是Exists的经典案例,由于Menu Item OldsAttributes之间存在多对多关系,因此略显复杂:

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]

答案 1 :(得分:0)

NonEmpty intersect可以作为替代方案吗?

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