SQL Server What is the equivalent of union in MDX?

1yjd4xko  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(107)

Im having to create a procedure/Function that returns an mdx query. This query is supposed to return a metric that is based on a year in a date range. EX- if a user asks for data from 2021-03-01 to 2023-02-28 it would look like this 20210301:20220228 and 20220301:20230228. The results returned would be an aggregate that looks similar to this.

I have been able to create several measures so far that return accurate numbers in the desired format, but because of the way that this one measure is calculated the same method doesn't work. But, I could overcome this problem if there were a way to union two select statements in mdx. I know there is a union function for mdx, but I don't think its exactly what I am looking for. Is there an mdx equivalent of the following code (yes I know this code won't work jut think of it as an illustration)

SELECT NON EMPTY {[Measures].[%Denial Rate] } ON COLUMNS
 FROM ( SELECT ( [Date - Remit Received].[Date Key].&[20210101] : [Date - Remit Received].[Date Key].&[20211231] ) ON COLUMNS 
 FROM ( SELECT ( { [Office ID].&[9] } ) ON COLUMNS 
 FROM [Remit])) WHERE ( [Office ID].&[9] ) 

 union all 

 SELECT NON EMPTY {[Measures].[%Denial Rate] } ON COLUMNS
 FROM ( SELECT ( [Date - Remit Received].[Date Key].&[20220101] : [Date - Remit Received].[Date Key].&[20221231] ) ON COLUMNS 
 FROM ( SELECT ( { [Office ID].&[9] } ) ON COLUMNS 
 FROM [Remit])) WHERE ( [Office ID].&[9] )

Is there a way to do this so that it returns in the format pictured above?

Here is the code I am currently using. It produced the results pictured above. It works, but it does not return accurate numbers

with  
           Set Denial_y1 as
           {([Denied Claim].[Denied Claim].&[Yes],[Date - Remit Received].[Date Key].[Date Key].&[20210101]:[Date - Remit Received].[Date Key].[Date Key].&[20211231])}
            
           set ClaimDetail_all1 as
           {([Date - Remit Received].[Date Key].[Date Key].&[20210101]:[Date - Remit Received].[Date Key].[Date Key].&[20211231])}
            
           Member [Measures].[(%Denial Rate_hd,Year 2021)] as
           sum(Denial_y1,Measures.[Distinct Claim Count_hd])/sum(ClaimDetail_all1,Measures.[Distinct Claim Count_hd])
            
        
           Set Denial_y2 as
           {([Denied Claim].[Denied Claim].&[Yes],[Date - Remit Received].[Date Key].[Date Key].&[20220101]:[Date - Remit Received].[Date Key].[Date Key].&[20221231])}
        
          
          set ClaimDetail_all2 as
           {([Date - Remit Received].[Date Key].[Date Key].&[20220101]:[Date - Remit Received].[Date Key].[Date Key].&[20221231])}
          
          Member [Measures].[(%Denial Rate_hd,Year 2022)] as
          sum(Denial_y2,Measures.[Distinct Claim Count_hd])/sum(ClaimDetail_all2 ,Measures.[Distinct Claim Count_hd])
          
          Select {[Measures].[(%Denial Rate_hd,Year 2021)],[Measures].[(%Denial Rate_hd,Year 2022)] } on 0
          from Remit
          where({[Office ID].[9]} )
iklwldmw

iklwldmw1#

MDX and SQL operate on completely different concepts, making direct translations impossible. In MDX you don't normally define new members as the direct calculation for a cell. You normally define them as some expression to then be applied on whatever cells are being queried.

From the working code you provide it looks like you can get the correct results by running this:

WITH  
  MEMBER [Measures].[%Denial Rate] 
    as ([Measures].[Distinct Claim Count_hd], [Denied Claim].[Denied Claim].&[Yes]) / ([Measures].[Distinct Claim Count_hd], [Denied Claim])
  MEMBER [Date - Remit Received].[Year 2021]
    as Aggregate([Date - Remit Received].[Date Key].[Date Key].&[20210101]:[Date - Remit Received].[Date Key].[Date Key].&[20211231])
  MEMBER [Date - Remit Received].[Year 2022]
    as Aggregate([Date - Remit Received].[Date Key].[Date Key].&[20220101]:[Date - Remit Received].[Date Key].[Date Key].&[20221231])

  SELECT 
    [Measures].[%Denial Rate] * {[Date - Remit Received].[Year 2021], [Date - Remit Received].[Year 2022]} on 0
  FROM [Remit]
  WHERE ([Office ID].[9])

But here I'm making the assumptions that your measures are all correctly defined (especially the measure Distinct Claim Count_hd) and that your Denied Claim dimension has a member that includes both denied and not denied claims.

Calculations in MDX are always expected to return the correct results. Any "small errors" usually mean your calculations are wrong. So do check that all numbers add up exactly to what they're supposed to.

相关问题