SQL 求和 - 然后加入 - 然后评论

SQL Sum - Then Join - Then Comment

我在让所有这些一起流动时遇到了一些问题。

我正在使用 Microsoft SQL 服务器。

我从 3 table 开始。

Table #1 - 人物

PersonId Name
1 Doug
2 Mary
3 Mike
4 Tim
5 Hank

Table #2 - 食品交易

FoodTransactionId PersonId Cost
1 1 50
2 1 80
3 2 15
4 3 25
5 3 30

Table #3 - 娱乐交易

EntertainmentTransactionId PersonId Cost
1 2 10
2 2 80
3 3 15
4 4 25
5 4 45
6 4 30

从这里开始,我的目标是进行汇总 table 或查看,包括每个人及其按交易类型的交易总和。

为了制作下面的视图,我 select 使用了一系列子 select。

我已经根据以上条件成功创建了以下table。

视图 #1 - 交易摘要

PersonId FoodTotal EntertainmentTotal
1 130
2 15 90
3 55 15
4 100

我最后的障碍在下面。我有一个 editable 评论 table 我想加入视图。

Table #4 - 评论

CommentId PersonId Comment
1 1 Here's a comment.
2 2 This is another comment.
3 3 How about this comment?
4 4 I like to comment.
5 5

下面的视图是将评论 table 加入视图 #1

的结果

视图 #2 - TransactionSummaryComment

PersonId FoodTotal EntertainmentTotal Comment
1 130 Here's a comment.
2 15 90 This is another comment.
3 55 15 How about this comment?
4 100 I like to comment.

一切正常。然后目标是显示此视图并允许用户编辑评论字段。

问题是,如果我从视图 #2 中编辑其中一条评论,我会收到以下错误:

“无法更新视图或函数,因为它包含聚合、DISTINCT 或 GROUP BY 子句、PIVOT 或 UNPIVOT 运算符。”

这让我认为我正在做的事情不会像这样工作,因为创建了聚合字段。

我想一定有一种方法可以达到预期的效果,但与我尝试过的方法不同。

任何关于实现此目标的更有效方法的想法或建议将不胜感激。

浏览文档显示以下内容 (https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15)

Updatable Views

You can modify the data of an underlying base table through a view, as long as the following conditions are true:

Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

The columns being modified in the view must directly reference the underlying data in the table columns. The columns cannot be derived in any other way, such as through the following:

    An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR, and VARP.

    A computation. The column cannot be computed from an expression that uses other columns. Columns that are formed by using the set operators UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also not updatable.

The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.

TOP is not used anywhere in the select_statement of the view together with the WITH CHECK OPTION clause.

请注意,Comment 的显示架构表明主键是 CommentId,并且没有 PersonId.

的唯一约束

这意味着每个 PersonId 可以有多个 Comment 记录。 如果这是预期的意图,那么为了根据上述限制使它们保持可编辑状态,我们将不允许 对它们进行分组/聚合。

以下将允许您编辑它们,但缺点是如果您有多个评论,每个 PersonId 可能有多行。

CREATE VIEW dbo.TransactionSummaryComment
AS
SELECT p.PersonId
,f.Cost [FoodTotal]
,e.Cost [EntertainmentTotal]
,c.Comment
FROM Person p
LEFT JOIN (SELECT PersonId, SUM(Cost) [Cost] FROM FoodTransaction GROUP BY  PersonId) f ON p.PersonId = f.PersonId
LEFT JOIN (SELECT PersonId, SUM(Cost) [Cost] FROM EntertainmentTransaction GROUP BY PersonId) e ON p.PersonId = e.PersonId
LEFT JOIN Comment c ON c.PersonId = p.PersonId
WHERE f.Cost IS NOT NULL
OR e.Cost IS NOT NULL

如果意图是每个 PersonId 只有一个评论,即 Comment 的架构将主键作为 PersonId,那么我们知道评论行将只有 每个人一个记录,因此视图不会重复。