# MDX示例：求解中位数、四分位数(median、quartile）

## 1、MDX中位数（Median）求解

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 //商业智能之路（jbean.org）,Begin：   WITH   //采集到的招聘广告条目（薪水测量值非空） SetRawAdvSetAS     NonEmpty([Advertisement].[Adv Id].members,[Measures].[Salary Sum])   //将广告条目按照薪水排序   SetAdvSetAS     Order(RawAdvSet, [Measures].[Salary Sum],DESC)   //招聘广告总数   Member [measures].[AdvCount] as     Count(AdvSet)   //招聘广告条目中间位置   Member [Measures].[MedianReal] as     ([measures].[AdvCount]-1) * 50 / 100   Member [Measures].[MedianInt] as     Int([Measures].[MedianReal])   Member [Measures].[MedianFrac] as     [Measures].[MedianReal]- [Measures].[MedianInt]   //薪水“中位数”（低）   Member [Measures].[MedianLow] as     ([AdvSet].Item([Measures].[MedianInt]).Item(0),[Measures].[SalarySum])   //薪水“中位数”（高）   Member [Measures].[MedianHigh] as     ([AdvSet].Item([Measures].[MedianInt] + 1).Item(0),[Measures].[Salary Sum])   //实际得到薪水的中位数   Member [Measures].[Salary Median] as     ([Measures].[MedianLow] * [Measures].[MedianFrac])     +([Measures].[MedianHigh] * (1 - [Measures].[MedianFrac]))   //商业智能之路（jbean.org）,End

## 2、四分位数（Quartile）求解

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // 1）求解1Q //商业智能之路（jbean.org）,Begin：   WITH   SetRawAdvSetAS     NonEmpty([Advertisement].[Adv Id].members,[Measures].[Salary Sum])   SetAdvSetAS     Order(RawAdvSet, [Measures].[Salary Sum],DESC)   Member [Measures].[1QReal] as     ([measures].[AdvCount]-1) * 25 / 100   Member [Measures].[1QInt] as     Int([Measures].[1QReal])   Member [Measures].[1QFrac] as     [Measures].[1QReal]- [Measures].[1QInt]   Member [Measures].[1QLow] as     ([AdvSet].Item([Measures].[1QInt]).Item(0),[Measures].[SalarySum])   Member [Measures].[1QHigh] as     ([AdvSet].Item([Measures].[1QInt] + 1).Item(0),[Measures].[Salary Sum])   //实际得到薪水的四分位数quantile（1Q）   Member [Measures].[Salary 1Q] as     ([Measures].[1QLow] * [Measures].[1QFrac])     +([Measures].[1QHigh] * (1 - [Measures].[1QFrac]))   // 2）求解3Q   //商业智能之路（jbean.org）,Begin： Member [Measures].[3QReal] as     ([measures].[AdvCount]-1) * 75 / 100   Member [Measures].[3QInt] as     Int([Measures].[3QReal])   Member [Measures].[3QFrac] as     [Measures].[3QReal]- [Measures].[3QInt]   Member [Measures].[3QLow] as     ([AdvSet].Item([Measures].[3QInt]).Item(0),[Measures].[SalarySum])   Member [Measures].[3QHigh] as     ([AdvSet].Item([Measures].[3QInt] + 1).Item(0),[Measures].[Salary Sum])   //实际得到薪水的四分位数quantile（3Q）   Member [Measures].[Salary 3Q] as     ([Measures].[3QLow] * [Measures].[3QFrac])     +([Measures].[3QHigh] * (1 - [Measures].[3QFrac]))   //商业智能之路（jbean.org）,End