MDX查询构建器实现

时间:2014-07-16 10:26:31

标签: .net sql-server mdx adomd.net

我正在使用Microsoft.AnalysisServices.AdomdClient来查询多维数据集。我有一个多维表达式(MDX)查询,我传递给AdomdCommand对象来查询我的多维数据集。 我想有一个MDX构建器类,我可以使用它来传递一个度量,维度,级别等列表,并生成相应MDX查询的字符串。

这是否已经存在?

2 个答案:

答案 0 :(得分:3)

这是一个良好的开端: http://www.c-sharpcorner.com/UploadFile/akrishnaraj/MdxQueryBuilder12052005041819AM/MdxQueryBuilder.aspx

如果您可以选择开源代码,那么此库包含一个MDX生成器: http://code.google.com/p/ranet-uilibrary-olap/

答案 1 :(得分:0)

要在模板上生成MDX查询,可以使用Ranet Olap。下面是一个简单的示例代码。它显示了典型的用例,包括过滤器设置和使用多个度量。

using System.Collections.Generic;
using System.Windows.Forms;
using Ranet.Olap.Core.Common;
using Ranet.Olap.Core.Data;
using Ranet.Olap.Core.Managers;
using Ranet.Olap.Core.Types;
using Ranet.Olap.Core.Wrappers;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            startWork();
        }

        public static void startWork()
        {

            var mdx = new QueryBuilderParameters
            {
                CubeName = "[Adventure Works]",
                SubCube = "",
                MdxDesignerSetting = new MDXDesignerSettingWrapper(),
                CalculatedMembers = new List<CalcMemberInfo>(),
                CalculatedNamedSets = new List<CalculatedNamedSetInfo>(),
                AreaWrappersFilter = new List<AreaItemWrapper>(),
                AreaWrappersColumns = new List<AreaItemWrapper>(),
                AreaWrappersRows = new List<AreaItemWrapper>(),
                AreaWrappersData = new List<AreaItemWrapper>()
            };

            //define parameters
            mdx.MdxDesignerSetting.HideEmptyColumns = false;
            mdx.MdxDesignerSetting.HideEmptyRows = false;
            mdx.MdxDesignerSetting.UseVisualTotals = false;
            mdx.MdxDesignerSetting.SubsetCount = 0;

            //columns
            var itemCol1 = new Hierarchy_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper,
                UniqueName = "[Date].[Calendar]"
            };
            mdx.AreaWrappersColumns.Add(itemCol1);

            //rows
            var itemRow1 = new Hierarchy_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper,
                UniqueName = "[Customer].[Customer Geography]"
            };
            mdx.AreaWrappersRows.Add(itemRow1);

            //data
            var itemData1 = new Measure_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.Measure_AreaItemWrapper,
                UniqueName = "[Measures].[Internet Order Count]"
            };
            mdx.AreaWrappersData.Add(itemData1);
            var itemData2 = new Measure_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.Measure_AreaItemWrapper,
                UniqueName = "[Measures].[Internet Sales Amount]"
            };
            mdx.AreaWrappersData.Add(itemData2);
            //if Measures in Data area > 1 - add special element VALUES in COLUMNS or ROWS areas
            var itemCol2 = new Values_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.NamedSet_AreaItemWrapper,
            };
            mdx.AreaWrappersColumns.Add(itemCol2);

            //filter
            var itemFilter1 = new Level_AreaItemWrapper
            {
                AreaItemType = AreaItemWrapperType.Level_AreaItemWrapper,
                UniqueName = "[Date].[Calendar Year].[Calendar Year]",
                HierarchyUniqueName = "[Date].[Calendar Year]",
                CompositeFilter = new Composite_FilterWrapper()
            };
            itemFilter1.CompositeFilter.IsUsed = true;
            itemFilter1.CompositeFilter.MembersFilter.IsUsed = true;


            // two options to choose
            // option 1 - simply SET
            //itemFilter1.CompositeFilter.MembersFilter.FilterSet = "{[Date].[Calendar Year].&[2007], [Date].[Calendar Year].&[2008]}";
            #region
            // option 2 - collection Members

            var mcf1 = new MemberChoiceSettings
            {
                SelectState = SelectStates.Selected_Self,
                Info =
                    new MemberData
                    {
                        UniqueName = "[Date].[Calendar Year].&[2007]",
                        HierarchyUniqueName = "[Date].[Calendar Year]",
                        Caption = "2007"
                    }
            };
            itemFilter1.CompositeFilter.MembersFilter.SelectedInfo.Add(mcf1);
            var mcf2 = new MemberChoiceSettings
            {
                SelectState = SelectStates.Selected_Self,
                Info =
                    new MemberData
                    {
                        UniqueName = "[Date].[Calendar Year].&[2008]",
                        HierarchyUniqueName = "[Date].[Calendar Year]",
                        Caption = "2008"
                    }
            };
            itemFilter1.CompositeFilter.MembersFilter.SelectedInfo.Add(mcf2);
            // build SET on collection
            itemFilter1.CompositeFilter.MembersFilter.BuildFilterSet();

            #endregion
            mdx.AreaWrappersFilter.Add(itemFilter1);

            string query = MdxQueryBuilder.Default.BuildQuery(mdx, null);
            MessageBox.Show(query);

        }
    }
}

结果将生成这样的MDX查询

SELECT 
HIERARCHIZE(CrossJoin(HIERARCHIZE([Date].[Calendar].Levels(0).Members), {[Measures].[Internet Order Count], [Measures].[Internet Sales Amount]})) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 0, 
HIERARCHIZE(HIERARCHIZE([Customer].[Customer Geography].Levels(0).Members)) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 1 
FROM 
(SELECT ({{[Date].[Calendar Year].&[2007], [Date].[Calendar Year].&[2008]}}) on COLUMNS FROM [Adventure Works]) 
CELL PROPERTIES BACK_COLOR, CELL_ORDINAL, FORE_COLOR, FONT_NAME, FONT_SIZE, FONT_FLAGS, FORMAT_STRING, VALUE, FORMATTED_VALUE, UPDATEABLE, ACTION_TYPE