英文:
How to configure automatic calculation in a column in oracle sql who's information is in another column?
问题
I have this Fact Table:
CREATE TABLE FACTPRODUCTSALES (
TRANSACTIONID NUMBER GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 ) PRIMARY KEY,
SALESINVOICENUMBER INT NOT NULL,
SALESDATEKEY NUMBER CONSTRAINT SALES_KEY_FK REFERENCES DIMDATE (DATEKEY),
SALESTIMEKEY NUMBER CONSTRAINT SALESTIME_KEY_FK REFERENCES DIM_TIME (TIMEKEY),
STOREID NUMBER NOT NULL CONSTRAINT STORE_ID_FK REFERENCES DIMSTORES (STOREID),
CUSTOMERID NUMBER NOT NULL CONSTRAINT CUSTOMER_ID_FK REFERENCES DIMCUSTOMER (CUSTOMERID),
PRODUCTID NUMBER NOT NULL CONSTRAINT PRODUCT_ID_FK REFERENCES DIMPRODUCT (PRODUCTKEY),
SALESPERSONID INT NOT NULL CONSTRAINT SALESPERSON_ID_FK REFERENCES DIMSALESPERSON (SALESPERSONID),
QUANTITY FLOAT,
SALESTOTALCOST INT as (DIMPRODUCT.PRODUCTSALESCOST * QUANTITY),
PRODUCTACTUALCOST INT as (DIMPRODUCT.PRODUCTACTUALCOST * QUANTITY),
DEVIATION FLOAT
);
在SALESTOTALCOST
和PRODUCTACTUALCOST
中,我想根据用户在DIMPRODUCT
字段中插入的ID来自动计算它们的值,依赖于DIMPRODUCT.PRODUCTSALESCOST
和DIMPRODUCT.PRODUCTACTUALCOST
中的值,但当然,这段代码不会起作用。
这可能吗?
英文:
I have this Fact Table:
CREATE TABLE FACTPRODUCTSALES (
TRANSACTIONID NUMBER
GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 )
PRIMARY KEY,
SALESINVOICENUMBER INT NOT NULL,
SALESDATEKEY NUMBER CONSTRAINT SALES_KEY_FK REFERENCES DIMDATE (DATEKEY),
SALESTIMEKEY NUMBER CONSTRAINT SALESTIME_KEY_FK REFERENCES DIM_TIME (TIMEKEY),
STOREID NUMBER NOT NULL CONSTRAINT STORE_ID_FK REFERENCES DIMSTORES (STOREID),
CUSTOMERID NUMBER NOT NULL CONSTRAINT CUSTOMER_ID_FK REFERENCES DIMCUSTOMER (CUSTOMERID),
PRODUCTID NUMBER NOT NULL CONSTRAINT PRODUCT_ID_FK REFERENCES DIMPRODUCT (PRODUCTKEY),
SALESPERSONID INT NOT NULL CONSTRAINT SALESPERSON_ID_FK REFERENCES DIMSALESPERSON ( SALESPERSONID ),
QUANTITY FLOAT,
SALESTOTALCOST INT as (DIMPRODUCT.PRODUCTSALESCOST * QUANTITY),
PRODUCTACTUALCOST INT as (DIMPRODUCT.PRODUCTACTUALCOST * QUANTITY),
DEVIATION FLOAT
);
As you can see in the SALESTOTALCOST
and PRODUCTACTUALCOST,
I want to automatically calculate their values each time I insert something on this table depending on the value inside of the DIMPRODUCT.PRODUCTSALESCOST
and DIMPRODUCT.PRODUCTACTUALCOST
depending on what's the ID inserted by the user in the field of DIMPRODUCT
but of course, this code wouldn't work.
Is this possible?
答案1
得分: 3
总的来说,不要这样做。
要么:
- 创建一个
VIEW
,通过将两个表连接起来在视图中生成总计数据。 - 如果你想要将数据实体化,那么创建一个
MATERIALIZED VIEW
。 - 当依赖列被修改时,使用(复合)触发器来更新表格。
您没有提供DIMPRODUCT
表的详细信息,以及这两个表如何关联,因此提供代码是不可能的。
英文:
In general, don't do that.
Either:
- Create a
VIEW
and generate the totals in the view by joining the two tables. - If you want to materialise the data then create a
MATERIALIZED VIEW
. - Use a (compound) trigger to update the table when the dependant columns are modified.
You do not provide details of the DIMPRODUCT
table or how the two tables are related so providing code is impossible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论