简体   繁体   中英

case statement filter in MDX query

I want to write the following T SQL query in MDX

Select count(bugs),priority from table 
where
Case when priority =1 then startdate< dateadd(dd,-7,getdate())
     when priority =2 then startdate< dateadd(dd,-14,getdate())
end
group by priority

Tried the following but not working

WITH MEMBER [Measures].CHECKING 
AS 
CASE [Item].[ startdate].CurrentMember 
WHEN [Item].[ Priority].&[1] THEN [Item].[startdate]<DATEADD(DAY,-7,NOW())
WHEN [Item].[ Priority].&[2] THEN [Item].[startdate]<DATEADD(DAY,-14,NOW())
END

SELECT
NON EMPTY{[Measures].[Count], [Measures].CHECKING }ON COLUMNS
,NON EMPTY{([Item].[ Priority].[ Priority].ALLMEMBERS )}

I am new to MDX queries, any suggestions on how to approach this please..

Your CASE logic has a basic problem. The statement cannot result in a condition. It can only result in a value that you then compare to something else.

To take your tSQL example, I think it should read more like this:

Select count(bugs),priority from table 
where
1 = Case when priority = 1 and startdate< dateadd(dd,-7,getdate()) Then 1
     when priority = 2 and startdate< dateadd(dd,-14,getdate()) then 1
     else 0 end
group by priority

A cleaner way to write this would be to skip the CASE altogether.

Select count(bugs),priority from table 
where
  (priority = 1 and startdate< dateadd(dd,-7,getdate()))
  or
  (priority = 2 and startdate< dateadd(dd,-14,getdate()))
group by priority

I am assuming the following:

  • Your startdate hierarchy is an attribute hierarchy, not a user hierarchy and
  • the current day is its last member.

Then the following MDX should deliver what you want:

SELECT
{ [Measures].[Count] }
ON COLUMNS
,
{  [Item].[ Priority].&[1], [Item].[ Priority].&[2] }
ON ROWS
FROM (
     SELECT ({ [Item].[ Priority].&[1] }
             *
             ([Item].[ startdate].[ startdate].Members
               - Tail([Item].[ startdate].[ startdate].Members, 7)
             )
            )
            +
            ({ [Item].[ Priority].&[2] }
             *
             ([Item].[ startdate].[ startdate].Members
               - Tail([Item].[ startdate].[ startdate].Members, 14)
             )
            )
     ON COLUMNS
     FROM [yourCube]
     )

Your code [Item].[startdate]<DATEADD(DAY,-7,NOW()) does not work in MDX for several reasons: firstly, [Item].[startdate] is a hierarchy, and hence cannot be compared using < . Secondly, even if you would re-state it as [Item].[startdate].CurrentMember < DATEADD(DAY,-7,NOW()) , you would have a member on the left side of the < , and a date , ie a value, on the right side. One of the important things to keep in mind with MDX are the different types of objects: Hierarchies, levels, members, tuples, sets. And all these are not values. You do not just have columns like in SQL.

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