简体   繁体   English

在 Azure 数据仓库 SQL 错误中创建物化视图

[英]Creating Materialized View in Azure Data Warehouse SQL error

I'm getting the error:我收到错误:

Cannot create the clustered index 'SalesByTerritory' on view 'Sample.wwi.SalesByTerritory' because it does not aggregate results.无法在视图“Sample.wwi.SalesByTerritory”上创建聚集索引“SalesByTerritory”,因为它不聚合结果。 Consider changing the view definition.考虑更改视图定义。

I'm trying to create a Materialized View in Azure SQL Data Warehouse (now Azure Synapse).我正在尝试在 Azure SQL 数据仓库(现为 Azure Synapse)中创建物化视图。 I'm just tring to create a view with the sales table and getting the territory name and date from another table.我只是想用销售表创建一个视图,并从另一个表中获取地区名称和日期。 Below is the code I'm using:下面是我正在使用的代码:

CREATE MATERIALIZED VIEW wwi.SalesByTerritory  
WITH (distribution = hash([Sale Key]))
AS
SELECT Sale.[Sale Key],
    Sale.[Description],
    Sale.[Quantity],
    Sale.[Profit],
    City.[Sales Territory],
    SaleDate.[Date],
    SaleDate.[Fiscal Month Label]
FROM wwi.fact_Sale Sale 
JOIN wwi.dimension_City City ON Sale.[City Key] = City.[City Key]
JOIN wwi.dimension_Date SaleDate ON Sale.[Invoice Date Key] = SaleDate.[Date]

That is probably because the Azure Synapse (DW) Materialized views can only work when there is some aggregation (probably to ensure uniqueness of some columns).这可能是因为 Azure Synapse (DW) 物化视图只能在存在某种聚合时才能工作(可能是为了确保某些列的唯一性)。 This page talks about needing either of the below 2 conditions to be true: 此页面讨论需要以下两个条件之一为真:

  1. The SELECT list contains an aggregate function. SELECT 列表包含一个聚合函数。
  2. GROUP BY is used in the Materialized view definition and all columns in GROUP BY are included in the SELECT list. GROUP BY 用于物化视图定义,GROUP BY 中的所有列都包含在 SELECT 列表中。 Up to 32 columns can be used in the GROUP BY clause. GROUP BY 子句中最多可以使用 32 列。

As from the official Microsoft documentation for Azure Synapse (formerly Azure SQL Data Warehouse), Aggregate functions are required in the SELECT list of the materialized view definition.从 Azure Synapse(以前称为 Azure SQL 数据仓库)的官方 Microsoft 文档中可以看出,物化视图定义的 SELECT 列表中需要聚合函数。 Supported aggregations include MAX, MIN, AVG, COUNT, COUNT_BIG, SUM, VAR, STDEV.支持的聚合包括 MAX、MIN、AVG、COUNT、COUNT_BIG、SUM、VAR、STDEV。

Also, the SELECT list in the materialized view definition needs to meet at least one of these two criteria:此外,物化视图定义中的 SELECT 列表需要至少满足以下两个条件之一:

  • The SELECT list contains an aggregate function. SELECT 列表包含一个聚合函数。
  • GROUP BY is used in the Materialized view definition and all columns in GROUP BY are included in the SELECT list. GROUP BY 用于物化视图定义中,GROUP BY 中的所有列都包含在 SELECT 列表中。 Up to 32 columns can be used in the GROUP BY clause. GROUP BY 子句中最多可以使用 32 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM