Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 创建索引视图_Sql_Sql Server_Sql Server 2008_Sql Server 2008 R2 - Fatal编程技术网

Sql 创建索引视图

Sql 创建索引视图,sql,sql-server,sql-server-2008,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008,Sql Server 2008 R2,我的表格结构如下: MyTable (ID Int, AccID1 Int, AccID2 Int, AccID3 int) ID AccID1 AccID2 AccID3 ---- -------- -------- -------- 1 12 2 NULL 2 4 12 1 3 NULL NULL

我的表格结构如下:

MyTable (ID Int, AccID1 Int, AccID2 Int, AccID3 int)

 ID      AccID1      AccID2      AccID3
----    --------    --------    --------
  1        12          2          NULL
  2        4           12           1
  3       NULL        NULL          5
  4        7          NULL          1   
我想创建具有以下输出的索引视图:

 ID    Level     Value
----   -----    -------
 1       1         12
 1       2         2 
 2       1         4
 2       2         12
 2       3         1
 3       3         5
 4       1         7
 4       3         1
编辑:

我的桌子非常大,我想有上面的输出。 我可以得到如下查询:

Select  ID,
        Case StrLevel
            When  'AccID1' Then 1
            When  'AccID2' Then 2
            Else 3
        End AS [Level],
        AccID as Value
From    (
        Select A.ID, A.AccID1, A.AccID2, A.AccID3
        From MyTable A
        )as p
UNPIVOT (AccID FOR [StrLevel] IN (AccID1, AccID2, AccID3)) AS unpvt

但上面的查询速度很慢,我希望索引视图具有更好的性能。
在索引视图中,我不能在索引视图中使用
UNION
UNPIVOT
CROSS-JOIN

如果您创建了一个数字表来完成非法的
CROSS-JOIN
的工作,该怎么办

Create Table Numbers (number INT NOT NULL PRIMARY KEY) 
Go

Insert Numbers 
Select top 30000 row_number() over (order by (select 1)) as rn
from sys.all_objects s1 cross join sys.all_objects s2
go

Create view v_unpivot with schemabinding
as  
Select MyTable.ID,
        n.number as [Level],
        Case n.number
            When 1 Then MyTable.AccID1
            When 2 Then MyTable.AccID2
            Else MyTable.AccID3
        End AS AccID
From dbo.Mytable
Join dbo.Numbers n on n.number BETWEEN 1 AND 3 
go

Create unique clustered index pk_v_unpivot on v_unpivot (ID, [Level]) 
go

Select 
  ID,
  [Level],
  AccID
From v_unpivot with (noexpand)
Where AccID IS NOT NULL 
Order by ID, [Level]

AccID不为NULL的
必须是查询的一部分,因为在索引视图中不允许使用派生表。

此外,虽然您的模式显示良好且清晰,但您实际要求的不是。请编辑问题,使您的问题更清楚,您到目前为止尝试了什么,以及在哪里遇到问题。@GarethD,请查看我的编辑。谢谢。您需要
UNPIVOT
。但我记不起来索引视图中是否允许这样做。假设所需结果中的行
2,3,5
的值是1而不是5。我在创建索引视图的查询中不使用
UNPIVOT
。我想升级到2012是不可能的?我注意到,
PIVOT
不再出现在中,尽管我还没有尝试过。正如(2008年)一样,您不太可能找到一个解决方法——大多数情况下,如果您遇到了一个限制,任何查询的重新工作都会遇到另一个限制。
    Select  A.ID,
            2 AS [Level],
            A.AccID1 AS AccID
     From MyTable A
     Where A.AccID1 IS NOT NULL

    Union

    Select  A.ID,
            2 AS [Level],
            A.AccID2
     From MyTable A
     Where A.AccID2 IS NOT NULL

    Union

    Select  A.ID,
            3 AS [Level],
            A.AccID3
     From MyTable A
     Where A.AccID3 IS NOT NULL  
Create Table Numbers (number INT NOT NULL PRIMARY KEY) 
Go

Insert Numbers 
Select top 30000 row_number() over (order by (select 1)) as rn
from sys.all_objects s1 cross join sys.all_objects s2
go

Create view v_unpivot with schemabinding
as  
Select MyTable.ID,
        n.number as [Level],
        Case n.number
            When 1 Then MyTable.AccID1
            When 2 Then MyTable.AccID2
            Else MyTable.AccID3
        End AS AccID
From dbo.Mytable
Join dbo.Numbers n on n.number BETWEEN 1 AND 3 
go

Create unique clustered index pk_v_unpivot on v_unpivot (ID, [Level]) 
go

Select 
  ID,
  [Level],
  AccID
From v_unpivot with (noexpand)
Where AccID IS NOT NULL 
Order by ID, [Level]