Mondrian的schema中,如何做到同一纬度的不同level数据作为查询条件来用?

本文探讨在Mondrian schema中遇到的问题,即如何在MDX查询中同时使用同一维度的不同level——model_name作为展示内容,model_id作为查询条件。作者分享了错误的MDX查询尝试以及最终通过修改schema和构建新的MDX语句成功实现需求的解决方案。
摘要由CSDN通过智能技术生成

如下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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值