mondrian schema一个简单的例子

说明:我会按照我的方式叙述,所以如果你看得不是很懂,希望你现在网上看一些关于mondrian schema和数据仓库的知识,再来回过头来看。

首先,创建数据库结构。

在这个tiny的系统中,数据库有3个表tb_employee(职员表),tb_time(时间表),tb_salary(薪酬表)。表结构如下:

drop table tb_employee;

create table tb_employee --职员维度表

(

     employee_id     number,             --职员id    

     employee_name   varchar2(10)        --职员姓名

);

drop table tb_time;

create table tb_time --时间维度表

(

    time_id   number,        --时间id

    the_year char(4),       --年

    the_month char(2)        --月

);

drop table tb_salary;

create table tb_salary --薪酬事实表

(

    employee_id number,                --职员id   

    time_id      number,                --时间id

    salary       number(19,4)           --薪酬

);

跟着是schema文件

<Schema name="Mondrian">

<Cube name="CubeTest">

    <Table name="TB_SALARY" />

    <Dimension name="Employee" foreignKey="EMPLOYEE_ID" >

        <Hierarchy hasAll="true" primaryKey="EMPLOYEE_ID">        

            <Table name="TB_EMPLOYEE" />

        <Level name="employeeId" column="EMPLOYEE_ID"   uniqueMembers="true" >

                <Property name="employeeName" column="EMPLOYEE_NAME"/>

            </Level>

        </Hierarchy>

    </Dimension>

    <Dimension name="Time" foreignKey="TIME_ID" >

        <Hierarchy hasAll="false" primaryKey="TIME_ID" >

            <Table name="TB_TIME" />

            <Level name="year" column="THE_YEAR" uniqueMembers="false" />

            <Level name="month" column="THE_MONTH" uniqueMembers="false" />

        </Hierarchy>

    </Dimension>       

    <Measure name="Salary" column="SALARY" aggregator="sum" />

</Cube>

</Schema>

schema文件是xml格式的,必须要有schema标签。cube标签就表明一个多维数据集合,dimension对应数据库中的维度表,measure对应数据库中的事实表中我们想要展示的列。dimension包含层(hierarchy),层包含级别(level)。

Hierarchy就是一个持有level关系mapping的结构,如:level [Year]、level [Month],如果没有在同一个hierarchy中指明它们之间的关系(Year是month的父),则它们都是独立的,它们的成员就不能体现出[2009].[1](2009年1月)的关系。 

同时在查询模型中,hierarchy又是一个查询point,它代表的是一类成员的划分集。如:当members作用于其上时,表示取该hierarchy下的所有成员(等价于其下所有level的成员),level就只是成员层次的一个划分子集。 “日-->月-->年” 中的日、月、年是分别是3个level,hierarchy描述的是(日-->月-->年)整个的关系。


最后是MDX查询:
select {[Measure].[Salary]} on columns
{[Employee].[employeeId].Members} on rows
from CubeTest
返回一个2维表,行是职员id,列是职员薪资的总和。

通过下面一个帖子,我明白了维度、层次、级别之间的关系,如果你感兴趣可以看看。

http://topic.csdn.net/u/20090821/11/b98976c7-c355-4e31-8e77-320a1522df83.html?1031830916

原文地址:https://www.cnblogs.com/iammatthew/p/1803877.html