sql - MS SQL : alter indexed view by including additional columns from new table

标签 sql sql-server tsql indexing pivot

我需要通过包含新创建的表中的其他列值来更新现有的 MS SQL 索引 View 。
索引 View :

CREATE OR ALTER VIEW [dbo].[MySelectionInfo]
  WITH schemabinding
  AS
    SELECT C.Id                                         id0,
           C.Code                                       Code1,
           C.Name                                       Name2,
           C.ProgramLevel                               Level3,
           C.Department                                 Department4,
           C.City                                       City10,
           C.STATE                                      State11,
           C.StartDate                                  StartDate12,
           C.Deadline                                   Deadline13,
           B.ID                                         Table_B_ID,
           A.Id                                        Table_A_ID

    FROM dbo.Table_A A
           INNER JOIN dbo.Table_B B ON A.id = B.Table_A_Id
           INNER JOIN dbo.Table_C C ON C.Table_B_Id = B.Id

新表:
CREATE TABLE [dbo].[Table_D] (
  [Id]               [int] IDENTITY (1, 1) PRIMARY KEY NOT NULL,
  [ModelName] [varchar](max)                    NOT NULL,
  [Table_C_Id]        [int]                             NOT NULL,
  [AttributeValue]   [varchar](max)                    NOT NULL,
  [CreatedDate]      [datetime]                        NOT NULL,
  [UpdatedDate]      [datetime]                        NOT NULL,
  CONSTRAINT FK_Table_C_Id FOREIGN KEY (Table_C_Id) REFERENCES some_schema.dbo.[Table_C] (Id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
)

新表中的数据:
enter image description here

我只想在 select * from [dbo].[MySelectionInfo] 中包含一些 ModelName 列值作为列名和 AttributeValue 作为值。结果集:

enter image description here

我可以使用 PIVOT 达到预期的结果功能:
CREATE OR ALTER VIEW [dbo].[MySelectionInfo]
  WITH schemabinding
  AS
    SELECT C.Id                                         id0,
           C.Code                                       Code1,
           C.Name                                       Name2,
           C.StartDate                                  StartDate12,
           C.Deadline                                   Deadline13,
           B.ID                                         Table_B_ID,
           A.Id                                        Table_A_ID
    FROM dbo.Table_A A
           INNER JOIN dbo.Table_B B ON A.id = B.Table_A_Id
           INNER JOIN dbo.Table_C C ON C.Table_B_Id = B.Id
           LEFT JOIN (SELECT PivotTable.Table_C_Id,
                             PivotTable.attribute1,
                             PivotTable.attribute2,
                             PivotTable.attribute3
                      FROM (SELECT Table_D.Table_C_Id,
                                   Table_D.ModelName,
                                   Table_D.AttributeValue
                            FROM dbo.Table_C
                                   INNER JOIN dbo.Table_D
                                     ON Table_C.Id = Table_D.Table_C_Id) AS sourceTable
                               PIVOT (
                                 Max(AttributeValue) FOR ModelName IN (attribute1, attribute2, attribute3)
                               ) AS PivotTable) dbo.Table_D D ON D.Table_C_Id = C.Id

但是 ,运行上面的SQL语句后,我无法为 View 创建聚集索引,因为LEFT JOIN , PIVOT , MAX禁止在 indexed views 中使用.

问题:是否有任何其他解决方案可以实现所需的结果并且仍然具有作为索引 View 的现有 View ?

最佳答案

如果您的 数据允许它 ,您可以使用 case 语句而不是 PIVOTing 进行交叉表:

/*
drop view dbo.mytestinfoview;
go
drop table dbo.Table1;
go
--*/

create table dbo.Table1
(
    id int,
    ModelName varchar(20),
    AttributeValue int
);


insert into dbo.Table1(id, ModelName, AttributeValue)
select distinct o.object_id, concat('attribute', v.id), o.attributevalue
from 
(
    select 
        object_id , 
        abs(checksum(newid())) % 2 as attributevalue
    from sys.objects
) as o
cross apply (values(1), (2), (3), (4)) as v(id)
go

create or alter view dbo.mytestinfoview
with schemabinding
as
    select id, count_big(*) as thecounter,
        sum(isnull(case ModelName when 'attribute1' then AttributeValue end, 0)) as attribute1,
        sum(isnull(case ModelName when 'attribute2' then AttributeValue else 0 end, 0)) as attribute2,
        sum(isnull(case ModelName when 'attribute3' then AttributeValue else 0 end, 0)) as attribute3
    from dbo.Table1
    group by id
go

create unique clustered index idx_v1 on dbo.mytestinfoview(id);
go

select * from dbo.mytestinfoview;
go

关于sql - MS SQL : alter indexed view by including additional columns from new table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59070076/

相关文章:

Mysql 数据透视表

sql - TSQL CASE WHEN 语法 - 使用替换

sql-server - 检查具有多个输入参数的约束 UDF 不起作用

sql-server - 如何使用 T-SQL 列出 Sql Server 2008 上的所有 SSIS 包

sql - 过程、函数或触发器中不允许使用 use 语句

sql - 在 SQL 语句中使用参数值

外网连接MYSQL

sql - T-Sql 按 MIN() 选择和分组

tsql - 删除表时,临时表上的索引是否被删除?

sql - sql语句语法错误