仅检索现有的维度组合

Retrieve only existing combinations of dimensions

这是一个包含 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", "国家代码") ?

与此 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 查询 returns 所有 9 种组合:

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

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

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

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

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

return correct/existing 组合的最简单方法是测量计数行 ('TableName')。现在引擎知道它必须使用关系。

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

您可以尝试使用 DaxStudio -> Server Timing 功能检查自己的情况。

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

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

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