1. 程式人生 > >Mondrian的schema中,如何做到同一緯度的不同level資料作為查詢條件來用?

Mondrian的schema中,如何做到同一緯度的不同level資料作為查詢條件來用?

如下schema程式碼片段:

<?xml version="1.0"  encoding="UTF-8" ?>
<Schema name="報表">
    <cube name="cube_qc_pass_item" caption="報表1" encoding="UTF-8">
        <table name="fact_qc_pass_record_item_join">
        <Dimension name="models" foreignKey="model_id" caption="模板">
            <Hierarchy
hasAll="true" allMemberName="model_name" primaryKey="id" primaryKeyTable="dim_qc_model">
<Table name="dim_qc_model" /> <Level name="model_name" column="name" caption="模板"/> <Level name="model_id" column="id" caption="model_id"/> </Hierarchy
>
</Dimension> <Measure name="times" column="id" aggregator="count" formatString="#,###0" datatype="Numeric" caption="總量"/> </cube> </Schema>

我想要在使用mdx查詢的時候,使用model_name顯示,使用model_id作為查詢條件限制某個model_id,該如何書寫mdx語句?

已經有如下錯誤的mdx語句了

mdx查詢語句1:

SELECT
NON EMPTY {Hierarchize(
{{[Measures].[times], [Measures].[notPass], [Measures].[pass]}})} ON COLUMNS, NON EMPTY {Hierarchize([models].[model_name].Members)} ON ROWS FROM [cube_qc_pass_model] where [models].[model_id].[5cda6afa-f837-4603-af27-0915cfb812fd]

如下查詢報錯:

MondrianException: Mondrian Error:Hierarchy '[models]' appears in more than one independent axis.

mdx查詢語句2:

SELECT
NON EMPTY {Hierarchize({{[Measures].[times], [Measures].[notPass], [Measures].[pass]}})} ON COLUMNS,
NON EMPTY Hierarchize(Union(CrossJoin([models].[model_name].Members, CrossJoin([times].[minute].Members, [agent_nos].[agent_no].Members)), CrossJoin([models].[model_id].[5cda6afa-f837-4603-af27-0915cfb812fd], CrossJoin([times].[minute].Members, [agent_nos].[agent_no].Members)))) ON ROWS
FROM [cube_qc_pass_model]

結果: 無效過濾

一直沒有找到好的解決辦法,變向實現了需求,來分享下:

首先改寫schema檔案,將model_name和model_id拆分成兩個維度:

<?xml version="1.0"  encoding="UTF-8" ?>
<Schema name="報表">
    <cube name="cube_qc_pass_item" caption="報表" encoding="UTF-8">
        <table name="fact_qc_pass_record_item_join">
        <Dimension name="models" foreignKey="model_id" caption="模板">
            <Hierarchy hasAll="true" allMemberName="model_name" primaryKey="id" primaryKeyTable="dim_qc_model">
                <Table name="dim_qc_model" />
                <Level name="model_name" column="name" caption="模板"/>
            </Hierarchy>
        </Dimension>
        <Dimension name="model_ids" foreignKey="model_id" caption="模板id">
            <Hierarchy hasAll="true" allMemberName="model_id" primaryKey="id" primaryKeyTable="dim_qc_model">
                <Table name="dim_qc_model" />
                <Level name="model_id" column="id" caption="model_id"/>
            </Hierarchy>
        </Dimension>
        <Measure name="times" column="id" aggregator="count" formatString="#,###0" datatype="Numeric" caption="總量"/>
    </cube>
</Schema>

使用如下語句查詢,是可以查詢到資料並實現了過濾的:

SELECT
NON EMPTY {Hierarchize({{[Measures].[times], [Measures].[notPass], [Measures].[pass]}})} ON COLUMNS,
NON EMPTY CrossJoin([models].[model_name].Members, CrossJoin([times].[minute].Members, [agent_nos].[agent_no].Members)) ON ROWS
FROM [cube_qc_pass_model] where [model_ids].[model_id].[5cda6afa-f837-4603-af27-0915cfb812fd]

by 劉迎光@螢火蟲工作室
OpenBI交流群:495266201
MicroService 微服務交流群:217722918
mail: liuyg#liuyingguang.cn
博主首頁(==防止爬蟲==):http://blog.csdn.net/gsying1474