需要一些帮助来制作具有 2 个 COUNT_BIG 的索引视图
好的,我正在尝试创建一个索引视图,该视图针对一个简单的表,该表存储人们认为对帖子有利/不利的结果。 这是对帖子投票的结果。
所以这是我的伪表:-
HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
PostId INT NOT NULL,
IsHelpful BIT NOT NULL,
UserId INT NOT NULL
所以用户每个帖子只能投一票。 它要么是 1(有帮助),要么是 0(无帮助)<-- 不确定是否有更好的方法来处理这个问题,如果有更好的方法的话。
好的。 我想做的是获得如下所示的视图。
HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
PostId INT NOT NULL,
IsHelpfulCount COUNT_BIG (WHERE IsHelpful = 1)
IsNotHelpfulCount COUNT_BIG (WHERE IsHelpful = 0)
最后,我需要使其可模式绑定,以便我可以在 PK 上添加一个索引,然后在 PostId 上添加一个索引。
我不知道创建视图的sql。 有什么建议么?
干杯:)
Ok, I'm trying to make an indexed view that is against a simple table that stores the results of what people think is good/bad for a post. This is the results of a thumbs up / thumbs down, voting on posts.
So here's my pseduo fake table :-
HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
PostId INT NOT NULL,
IsHelpful BIT NOT NULL,
UserId INT NOT NULL
So a user can only have one vote per post. It's either a 1 (helpful) or 0 (unhelpful) <-- not sure of a better way to handle that, if there is a better way.
Ok.
What i'm trying to do is get a view that looks like the following.
HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
PostId INT NOT NULL,
IsHelpfulCount COUNT_BIG (WHERE IsHelpful = 1)
IsNotHelpfulCount COUNT_BIG (WHERE IsHelpful = 0)
And finally, i'll need to make it schemabindable so i can add an index on the PK and then an index on the PostId.
I have no idea about the sql to make the view. Any suggestions?
Cheers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想法:
还有其他 索引视图的限制
编辑:
删除了“身份”字段,该字段被意外添加到原始问题/帖子中。
编辑2(GBN):
我忘记了索引视图中的任何聚合也需要 COUNT_BIG(*)。 因此,只需添加一个作为虚拟列即可。 我已经测试过这个。
Thoughts:
There are other limitations of indexed views
Edit:
Removed the Identity field, which was accidently added to the original question/post.
Edit 2 (gbn):
I forgot that any aggregate in an indexed view also needs a COUNT_BIG(*). So, simply add one as a dummy column. I've tested this.