我在SQL Server 2005中有以下表模式,我想将其反规范化为我正在编写的一些查询的索引视图,直到我可以使结构更改成为永久性。
表格如下
Writing
(
DocumentSerial int
Grader1_ID int
Grade_1 int
Grader2_ID int
Grade_2 int
Grader3_ID int
Grade_3 int
)
Users
(userID int,
firstname,
lastname
)
我想为每个评分者/成绩/文件组合提供一个单行的表格,其中评分者是评分者1,评分者2或评分者3
我编写的View使用UNION,因此它不会索引:
select documentSerial,grader1_id as grader_id, grade_1 as grade ,1 as sequence
from Writing w inner join User U on w.grader1_id=u.userid
UNION
select documentSerial,grader2_id as grader_id, grade_2 as grade ,2 as sequence
from Writing w inner join User U on w.grader2_id=u.userid
UNION
select documentSerial,grade31_id as grader_id, grade_3 as grade ,3 as sequence
from Writing w inner join User U on w.grader3_id=u.userid
问题是 - SQL无法使用union索引视图...
答案 0 :(得分:1)
为什么在使用正确的表时必须是索引视图:
SELECT x.*
INTO normalized_table
FROM (select documentSerial,grader1_id as grader_id, grade_1 as grade ,1 as sequence
from Writing w
join User U on w.grader1_id = u.userid
UNION ALL
select documentSerial,grader2_id as grader_id, grade_2 as grade ,2 as sequence
from Writing w
join User U on w.grader2_id = u.userid
UNION ALL
select documentSerial,grade31_id as grader_id, grade_3 as grade ,3 as sequence
from Writing w
join User U on w.grader3_id = u.userid) x
我知道你可能想要这个视图,所以你没有同步数据的麻烦。唯一的另一种选择是不对视图编制索引......