引用当前行
我有以下表格:
TableA
ID | SomeInt
1 55
1 66
2 77
TableB
ID | OtherInt
1 ComputedBy Field
2 ComputedBy Field
计算字段需要从 tableA 返回总和,其中 TableB.ID = TableA.ID,但如果我说:
SELECT SUM(SomeInt) from TableA where ID = TableA.Id
其中第一个 ID 是当前表的 ID,则 dbms 会说“之间的模糊字段名称table Table1 和 Table2"
那么,如何从当前行获取 ID,以便它不会混合字段名称。
像 This.ID、Self.ID、CurrentRow.ID 或任何 sql 使用的东西
编辑:显然我可以使用当前表的名称来引用列
I have the following tables :
TableA
ID | SomeInt
1 55
1 66
2 77
TableB
ID | OtherInt
1 ComputedBy Field
2 ComputedBy Field
The computed by field needs to return the sum from tableA where TableB.ID = TableA.ID, but if I say:
SELECT SUM(SomeInt) from TableA where ID = TableA.Id
where the first ID would be the current table's ID, the dbms says "Ambiguous field name between table Table1 and Table2"
So, how do I get the ID from the current row so that it doesnt mix the field name.
Something like This.ID, Self.ID, CurrentRow.ID or whatever sql uses
EDIT: Apparently I could use Current table's name to reference the column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的答案
这是一个完全解决您的问题借口 tsql 语法的示例。
Simple answer
This is a fully worked out example of your problem excuse tsql syntax.
您是否试图使 TableB 具有 TableA 值的总和? ID 是否已经存在(只需更新)或者您需要从头开始填充 TableB?我不明白你的总体目标是什么,这会影响答案。
EG
更改表A
让我知道您的目标是什么,我们可以获得理想的答案。
Are you trying to make TableB have the sum of TableA's values? Do the ID's already exist (just need to be updated) or do you need to populate TableB from scratch? I don't understand what your overall goal is, and that would affect the answer.
E.G.
changes to tableA
Let me know what your goal is and we can get an ideal answer.
您可以简单地将
"TableB"
本身设置为计算视图:You might simply make
"TableB"
itself a computed view: