使用不同 table SQL 添加计算列
Adding a calculated column using different table SQL
我是 SQL 的新手,所以请多多包涵。
我正在尝试将计算列 InvoiceAmount
添加到我的 CompanyInvoice
table。
我的table目前有以下列:
CREATE TABLE CompanyInvoice
(
InvoiceID CHAR(5) NOT NULL,
CompanyAccountID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_CompanyAccount
FOREIGN KEY REFERENCES CompanyAccount(CompanyAccountID)
ON UPDATE CASCADE,
CompanyID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_ProductionCompany
FOREIGN KEY REFERENCES ProductionCompany(CompanyID),
BookingID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_EventBooking
FOREIGN KEY REFERENCES EventBooking (BookingID),
TheatreID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_Theatre
FOREIGN KEY REFERENCES Theatre(TheatreID)
ON UPDATE CASCADE
);
我想添加一个计算发票金额的新列 - 发票金额的计算取决于事件的持续时间(EventDuration
列保存在 Event
table 中)和 TheatreID
,例如,如果 TheatreID=1
,则 EventDuration x 300
,如果 TheatreID=2
,则 EventDuration x 1000
。
非常感谢
如您所述,视图看起来像
create view v_invoice as
select c.InvoiceID,
e.EventDuration * case when c.TheatreID = 1 then 300
when c.TheatreID = 2 then 1000
end as invoice_amount
from CompanyInvoice c join ... ??? unknown; how to get to Event table?
如您所见,???
意味着您没有描述如何从我们知道的数据中获取Event
table。
另一方面,将任何值与硬编码值 (300
、1000
) 相乘可能不是一个好主意,因为 - 如果它们发生变化怎么办?您会修改您编写的所有代码以实现这些更改吗?根据你目前所说的,这些应该是 Theatre
table 中的另一列(或者新的 table,如果你想跟踪随时间的变化;新的 table 会与 Theatre
).
有亲子关系
像这样(注意我还在猜测加入条件):
create view v_invoice as
select c.InvoiceID,
e.EventDuration * t.value as invoice_amount
-------
this, instead of hardcoded 300 or 1000 or ...
from CompanyInvoice c join Theatre t on t.TheatreID = c.TheatreID
join Event e on e.EventID = t.EventID
我是 SQL 的新手,所以请多多包涵。
我正在尝试将计算列 InvoiceAmount
添加到我的 CompanyInvoice
table。
我的table目前有以下列:
CREATE TABLE CompanyInvoice
(
InvoiceID CHAR(5) NOT NULL,
CompanyAccountID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_CompanyAccount
FOREIGN KEY REFERENCES CompanyAccount(CompanyAccountID)
ON UPDATE CASCADE,
CompanyID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_ProductionCompany
FOREIGN KEY REFERENCES ProductionCompany(CompanyID),
BookingID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_EventBooking
FOREIGN KEY REFERENCES EventBooking (BookingID),
TheatreID CHAR(5) NOT NULL
CONSTRAINT FK_CompanyInvoice_Theatre
FOREIGN KEY REFERENCES Theatre(TheatreID)
ON UPDATE CASCADE
);
我想添加一个计算发票金额的新列 - 发票金额的计算取决于事件的持续时间(EventDuration
列保存在 Event
table 中)和 TheatreID
,例如,如果 TheatreID=1
,则 EventDuration x 300
,如果 TheatreID=2
,则 EventDuration x 1000
。
非常感谢
如您所述,视图看起来像
create view v_invoice as
select c.InvoiceID,
e.EventDuration * case when c.TheatreID = 1 then 300
when c.TheatreID = 2 then 1000
end as invoice_amount
from CompanyInvoice c join ... ??? unknown; how to get to Event table?
如您所见,???
意味着您没有描述如何从我们知道的数据中获取Event
table。
另一方面,将任何值与硬编码值 (300
、1000
) 相乘可能不是一个好主意,因为 - 如果它们发生变化怎么办?您会修改您编写的所有代码以实现这些更改吗?根据你目前所说的,这些应该是 Theatre
table 中的另一列(或者新的 table,如果你想跟踪随时间的变化;新的 table 会与 Theatre
).
像这样(注意我还在猜测加入条件):
create view v_invoice as
select c.InvoiceID,
e.EventDuration * t.value as invoice_amount
-------
this, instead of hardcoded 300 or 1000 or ...
from CompanyInvoice c join Theatre t on t.TheatreID = c.TheatreID
join Event e on e.EventID = t.EventID