使用 SELECT 引用计算列中的当前行
我有一个表格,表示合同的参数 - 包括它们通过附录随时间的变化。第一个附录是“特殊”附录,代表合同首次签订时的参数。
该表应如下所示:
ID ProjectID BeginDate DeadlineMonths DeadlineDate
1 20 20-12-2006 24 <computed= 20-12-2006 + 24 months>
2 23 12-03-2007 12 <computed= 12-03-2007 + 12 months>
3 20 06-09-2007 36 <computed= **20-12-2006** + 36 months>
ProjectID 是 Projects 表的 FK,其主键也称为 ProjectID。
我希望 DeadlineDate 成为一个计算字段,计算方式如下:
DeadlineDate COMPUTE BY ((
select first 1 AddMonth(contract.BeginDate, DeadlineMonths)
from addendums contract
where contract.projectid = projectid
order by contract.BeginDate ))
问题是在 contract.projectid = projectid
中,第二个 ProjectID 必须引用正在计算的当前行,而不是 select 语句中的当前行(与 contract.projectid
相同)。
我正在使用火鸟。我需要表中的列,而不是 SELECT 语句中的列,因为使用数据库的应用程序中存在 ORM 问题。
I have a table that represents the parameters of a contract - including their change over time through addendums. The first addendum is a "special" addendum representing the parameters when the contract was first signed.
Here's how the table should look like:
ID ProjectID BeginDate DeadlineMonths DeadlineDate
1 20 20-12-2006 24 <computed= 20-12-2006 + 24 months>
2 23 12-03-2007 12 <computed= 12-03-2007 + 12 months>
3 20 06-09-2007 36 <computed= **20-12-2006** + 36 months>
ProjectID is a FK to the Projects table whose primary key is also called ProjectID.
I want DeadlineDate to be a calculated field, calculated like so:
DeadlineDate COMPUTE BY ((
select first 1 AddMonth(contract.BeginDate, DeadlineMonths)
from addendums contract
where contract.projectid = projectid
order by contract.BeginDate ))
The problem is that in contract.projectid = projectid
the second ProjectID has to reference the current row being computed, not the current row in the select statement (which is the same as contract.projectid
).
I'm using Firebird. I need the column in the table and NOT in a SELECT statement because of ORM issues in the application using the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在字段前面加上当前表的表名即可:
Just prefix the field with table name of the current table:
您可以为您的查询创建一个视图并在
ORM
中使用它吗?Can you create a view over your query and use it in the
ORM
?