如何在Oracle SQL中配置另一列中的信息以进行自动计算?

huangapple go评论55阅读模式
英文:

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
);

SALESTOTALCOSTPRODUCTACTUALCOST中,我想根据用户在DIMPRODUCT字段中插入的ID来自动计算它们的值,依赖于DIMPRODUCT.PRODUCTSALESCOSTDIMPRODUCT.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

总的来说,不要这样做。

要么:

  1. 创建一个VIEW,通过将两个表连接起来在视图中生成总计数据。
  2. 如果你想要将数据实体化,那么创建一个MATERIALIZED VIEW
  3. 当依赖列被修改时,使用(复合)触发器来更新表格。

您没有提供DIMPRODUCT表的详细信息,以及这两个表如何关联,因此提供代码是不可能的。

英文:

In general, don't do that.

Either:

  1. Create a VIEW and generate the totals in the view by joining the two tables.
  2. If you want to materialise the data then create a MATERIALIZED VIEW.
  3. 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.

huangapple
  • 本文由 发表于 2023年6月8日 17:43:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定