我在讓所有這些一起流動時遇到了一些問題。
我正在使用 Microsoft SQL Server。
我從 3 張桌子開始。
表 #1 - 人
| 人名 | 名稱 |
|---|---|
| 1 | 道格 |
| 2 | 瑪麗 |
| 3 | 麥克風 |
| 4 | 蒂姆 |
| 5 | 漢克 |
表 #2 - FoodTransaction
| 食品交易ID | 人名 | 成本 |
|---|---|---|
| 1 | 1 | 50 |
| 2 | 1 | 80 |
| 3 | 2 | 15 |
| 4 | 3 | 25 |
| 5 | 3 | 30 |
表 #3 - EntertainmentTransaction
| 娛樂交易ID | 人名 | 成本 |
|---|---|---|
| 1 | 2 | 10 |
| 2 | 2 | 80 |
| 3 | 3 | 15 |
| 4 | 4 | 25 |
| 5 | 4 | 45 |
| 6 | 4 | 30 |
從這里開始,我的目標是制作一個匯總表或視圖,包括每個人以及他們按交易型別劃分的交易總額。
為了制作下面的視圖,我選擇了一系列子選擇。
我從 Person 表和 FoodTransaction 表之間的完整外部連接開始。我創建了一個新欄位 sum(FoodTransaction.Cost) 作為 FoodTotal。
然后我在這些結果和 EntertainmentTransaction 表之間做一個左連接,在那里我創建一個新的欄位 sum(EntertainmentTransaction.Cost) 作為 EntertainmentTotal。
然后我添加一個 where 子句,說明其中 FoodTotal 不為空或 EntertainmentTotal 不為空。這個 where 子句從 Person 表中洗掉了在任一表上都沒有事務的所有條目。
我已根據上述標準成功創建了下表。
視圖 #1 - TransactionSummary
| 人名 | 食物總計 | 娛樂總計 |
|---|---|---|
| 1 | 130 | |
| 2 | 15 | 90 |
| 3 | 55 | 15 |
| 4 | 100 |
我的最后一個障礙在下面。我有一個可編輯的評論表,我想將其加入視圖。
表 #4 - 評論
| 評論編號 | 人名 | 評論 |
|---|---|---|
| 1 | 1 | 這是評論。 |
| 2 | 2 | 這是另一個評論。 |
| 3 | 3 | 這個評論怎么樣? |
| 4 | 4 | 我喜歡評論。 |
| 5 | 5 |
下面的視圖是將 Comment 表連接回視圖 #1 的結果
視圖 #2 - TransactionSummaryComment
| 人名 | 食物總計 | 娛樂總計 | 評論 |
|---|---|---|---|
| 1 | 130 | 這是評論。 | |
| 2 | 15 | 90 | 這是另一個評論。 |
| 3 | 55 | 15 | 這個評論怎么樣? |
| 4 | 100 | 我喜歡評論。 |
這一切都有效。目標是顯示此視圖并允許用戶編輯評論欄位。
The problem is if I go to edit one of the comments from within View #2 I get the following error:
"Cannot update the view or function because it contains aggregates, or a DISTINCT or GROUP BY clause, or PIVOT or UNPIVOT operator."
This leads me to think that what I'm doing won't work like this because of the aggregate fields which were created.
I imagine there must be a way to achieve the desired results different from the method I have tried.
Any thoughts or suggestions on a more efficient way of achieving this would be greatly appreciated.
uj5u.com熱心網友回復:
瀏覽檔案顯示以下內容(https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver15)
可更新的視圖您可以通過視圖修改底層基表的資料,只要滿足以下條件即可:
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 可以有多個評論記錄。如果這是所需的意圖,那么為了根據上述限制使它們可編輯,我們將不允許對它們進行分組/聚合。
以下將允許您編輯它們,但缺點是如果您有多個評論,每個 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,那么我們知道每個 Person 的評論行只有一條記錄,因此視圖不會重復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379300.html
標籤:sql sql-server join view aggregate
上一篇:對于少數呼叫者,RESTAPI呼叫為500,但對于其他呼叫者來說作業正常
下一篇:如何提取上個月的資料
