【问题标题】:Retrieve only existing combinations of dimensions仅检索现有的维度组合
【发布时间】:2022-01-02 22:09:18
【问题描述】:

这是一个基本模型,其中 2 个表以 1-1 关系:

CREATE TABLE CountriesNames(countryId INT, countryName VARCHAR(50));
INSERT INTO CountriesNames VALUES
    (1, 'France'),
    (2, 'Germany'),
    (3, 'Italy');

CREATE TABLE CountriesCodes(countryId INT, countryCode VARCHAR(50));
INSERT INTO CountriesCodes VALUES
    (1, 'FR'),
    (2, 'DE'),
    (3, 'IT');

现在创建一个 SSAS 表格 模型

{
  "create": {
    "database": {
      "name": "TEST_CUBE",
      "compatibilityLevel": 1500,
      "model": {
        "name": "Sales",
        "culture": "en-US",
        "dataSources": [
          {
            "name": "DS",
            "connectionString": "Provider=SQL Server Native Client 11.0;Data Source=.\\MSSQLSERVER01;Integrated Security=SSPI;Initial Catalog=TEST_CUBE",
            "impersonationMode": "impersonateServiceAccount"
          }
        ],
        "tables": [
          {
            "name": "Country Name",
            "columns": [
              {
                "name": "Country ID",
                "dataType": "int64",
                "isHidden": true,
                "sourceColumn": "countryId"
              },
              {
                "name": "Country Name",
                "dataType": "string",
                "sourceColumn": "countryName"
              }
            ],
            "partitions": [
              {
                "name": "Partition",
                "mode": "import",
                "source": {
                  "type": "query",
                  "query": "SELECT * FROM CountriesNames",
                  "dataSource": "DS"
                }
              }
            ]
          },
          {
            "name": "Country Code",
            "columns": [
              {
                "name": "Country ID",
                "dataType": "int64",
                "isHidden": true,
                "sourceColumn": "countryId"
              },
              {
                "name": "Country Code",
                "dataType": "string",
                "sourceColumn": "countryCode"
              }
            ],
            "partitions": [
              {
                "name": "Partition",
                "mode": "import",
                "source": {
                  "type": "query",
                  "query": "SELECT * FROM CountriesCodes",
                  "dataSource": "DS"
                }
              }
            ]
          }
        ],
        "relationships": [
          {
            "name": "Relation",
            "fromTable": "Country Name",
            "fromColumn": "Country ID",
            "toTable": "Country Code",
            "toColumn": "Country ID"
          }
        ]
      }
    }
  }
}

如何使用 MDX 查询模型以仅获取 ("Country Name"、"Country Code") 的 3 种可能组合?

与此 SQL 查询的结果相同:

SELECT
    countryName,
    countryCode
FROM
    CountriesNames cn JOIN
    CountriesCodes cc ON cc.countryId = cn.countryId

这给出了:

Country Name | Country Code
---------------------------
France       | FR
Germany      | DE
Italy        | IT

而这个简单的 MDX 查询返回所有 9 种组合:

SELECT
    ([Country Name].Children, [Country Code].Children) ON 0
FROM Sales

我怀疑措施是强制性的,但如果是这样,我想知道为什么,可能遗漏了一些明显的东西。

【问题讨论】:

    标签: ssas mdx olap cube ssas-tabular


    【解决方案1】:

    你说得对,措施是强制性的;这是因为引擎以这种方式工作 - 决定引擎设计级别(如 DAX 和 MDX): 如果您不选择度量,那么 Tablular 会在底层生成两个没有连接的单独查询(例如这个) - 并生成交叉连接:

    SET DC_KIND="AUTO";
    SELECT
    'CountriesCodes'[countryCode]
    FROM 'CountriesCodes';
    
    SET DC_KIND="AUTO";
    SELECT
    'CountriesNames'[countryName]
    FROM 'CountriesNames';
    

    返回正确/现有组合的最简单方法是测量 countrows('TableName')。现在引擎知道它必须使用关系。

    SET DC_KIND="AUTO";
    SELECT
    'CountriesNames'[countryName], 'CountriesCodes'[countryCode],
    COUNT (  )
    FROM 'CountriesCodes'
        LEFT OUTER JOIN 'CountriesNames' ON 'CountriesCodes'[countryId]='CountriesNames'[countryId];
    

    您可以尝试使用 DaxStudio -> 服务器计时功能检查自己发生了什么。

    一件重要的事情! 如果您从一个表中查询列(然后触发自动存在的行为)。

    我们得到 10 次出现中的 5 次。

    https://www.sqlbi.com/articles/understanding-dax-auto-exist/

    【讨论】:

    • 感谢您的回答和背景信息。 :)
    猜你喜欢
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2017-10-07
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多