简体   繁体   English

事实表变更

[英]Fact table changes

CONTEXT 语境

I'm trying to create a sales fact table. 我正在尝试创建一个销售情况表。 Each row represent a product from an order for a customer. 每行代表客户订单中的产品。 Among other fields, I have these 3 specific fields: 在其他领域中,我有以下3个具体领域:

  • purchasing_price 采购价格
  • selling_price selling_price
  • margin 余量

PROBLEM 问题

In our company, when we sell a product, I don't know exactly the purchasing price, because I will pay this product later and the purchasing price will be based on the exchange rate at the payment date. 在我们公司中,当我们销售产品时,我不知道确切的购买价格,因为我稍后会付款,并且购买价格将基于付款日期的汇率。 I usually know the real purchasing price between 1 week and 2 month after the order date. 我通常知道订购日期后1周到2个月之间的实际购买价格。 Indeed, my firm got an arrangement that allow us to pay our suppliers each 15th of the next month, from the month when we receive the product from our supplier. 确实,我的公司已做出安排,使我们能够从收到供应商产品之月起,在下个月的每个15日向供应商付款。 Since we have suppliers in different countries the exchange rate raise some issues. 由于我们在不同国家/地区都有供应商,因此汇率带来了一些问题。

RESULTS AWAITING 等待结果

I had to generate 2 monthly reports and 1 annual report : - 1 report on the 1st of each month based on the exchange rate of the order date - 1 report on the 15th of each month based on the exchange rate of the payment date (which is the exchange rate of the current date because we pay our suppliers each 15th of the month) - 1 annual report based on the exchange rate of the payment date (which could sometimes be 2 month after the order date) 我必须生成2个月度报告和1个年度报告:-每个月1日的1个报告基于订单日期的汇率-每个月15日的1个报告基于付款日期的汇率(其中是当前日期的汇率,因为我们会在每个月的15号向供应商付款。-1个基于付款日期汇率的年度报告(有时可能是订购日期后的2个月)

EXAMPLE

  1. I order a product on 3rd July. 我在7月3日订购了产品。
  2. This product is only delivered on the 7th August 该产品仅在8月7日交付
  3. Then I will pay the product the 15th September based on the exchange rate of this date. 然后,我将根据该日期的汇率在9月15日向产品付款。

SOLUTIONS 解决方案

So far, I find only 3 solutions: 到目前为止,我仅找到3个解决方案:

  1. a) create 1 row in the fact table and 2 fields: real_purchasing_price (which would be equal to 0) and temporary_purchasing_price (which would automatically be equal to the purchasing price based on the exchange rate of the order date). a)在事实表中创建1行,并在2个字段中创建: real_purchasing_price (将等于0)和临时 _purchasing_price (将根据订单日期的汇率自动等于购买价格)。 b) once I paid the product, I know the right exchange rate, therefore I can update this row an fulfill the field real_purchasing_price with the purchasing price based on the exchange rate of the payment. b)一旦我付款了产品,我就知道正确的汇率,因此我可以更新该行,并根据付款的汇率用购买价格填写real_purchasing_price字段。

  2. a) create 1 row in the fact table with the purchasing_price based on the exchange rate of the order date. a)根据订单日期的汇率在事实表中使用purchase_price创建1行。 b) once I paid the product, I know the right exchange rate therefore I can create 1 new row in the fact table almost similar to the first one, but this time with the purchasing_price based on the exchange rate of the payment date. B)当我付的产品,我知道正确的汇率,因此我可以创建在几乎相同的第一个事实表1个新行,但根据付款日期的汇率这次与purchasing_price。

  3. a) create a row in the fact table with the purchasing_price based on the exchange rate of the order date b) once I paid the product I know the right exchange rate therefore I can update this row and replace the purchasing_price by the one based on the right exchange rate. 一)创建基于订单日期B的汇率purchasing_price事实表中的行),一旦我支付的产品,我知道正确的汇率,因此我可以更新该行并通过基于一个更换purchasing_price正确的汇率。

The 4th solution belongs to you. 第四个解决方案属于您。

Thx for your help. 谢谢您的帮助。 Don't hesitate to ask me about more details. 不要犹豫,向我询问更多详细信息。

Have a good day, 祝你有美好的一天,

I actually choose to use a currency table and to add a field payment_date which allow me to match each row of the fact table with right exchange rate in the currency table. 我实际上选择使用货币表并添加一个字段payment_date,该字段使我可以将事实表的每一行与货币表中的正确汇率进行匹配。

Nevertheless, I have to UPDATE each row of the fact table to add the payment_date once I know it. 但是,我必须更新事实表的每一行以在我知道后添加payment_date。

I couldn't find a better solution so far. 到目前为止,我找不到更好的解决方案。

Thx everyone. 谢谢大家。

I am assuming that for purchasing price, you have a componet which is fixed and another which is variable and depends on exchange rate. 我假设对于购买价格,您有一个固定的组件,另一个是可变的,取决于汇率。 (say p*x where x is exchange rate) (例如p * x,其中x是汇率)

I would suggest to create a new fact table, say exchange_rates with different countries exchange rates and timestamp. 我建议创建一个新的事实表,说exchange_rates与不同国家的汇率和时间戳。 Now the column purchasing_price in your fact table, should be a result of join from exchange_rates table for that country. 现在列purchasing_price在事实表,应该是一个结果,从加入exchange_rates表该国。

Lets say current_values column hold exchange rate. 假设current_values列保存汇率。

    EXCHANGE_RATES
    +-------------+---------------+---------------+-----------+
    | EXCHANGE_ID | EXCHANGE_NAME | CURRENT_VALUE | TIMESTAMP |
    +-------------+---------------+---------------+-----------+
    |           1 | JAPAN         |           100 |  20151021 |
    +-------------+---------------+---------------+-----------+     

While populating purchasing price, use this value from EXCHANGE_RATES to get correct values. 在填充购买价格时,请使用EXCHANGE_RATES该值来获取正确的值。

    ORDER_DETAIL_FACT       
    +----------+---------+------------------+---------------+--------+
    | ORDER_ID | PRODUCT | PURCHASING_PRICE | SELLING_PRICE | MARGIN |
    +----------+---------+------------------+---------------+--------+
    |      101 |      11 | 50*100           |          6000 |   1000 |
    +----------+---------+------------------+---------------+--------+

Let me know if you need help with its implementation. 让我知道您是否需要有关其实施的帮助。

It seems your order goes through three stages: 您的订单似乎经历了三个阶段:

  • ordered 下令

  • delivered 已交付

  • purchasing price is known 购买价格已知

One data-warehouse design approach is the immutability (insert only,make no updates). 一种数据仓库设计方法是不变性 (仅插入,不进行更新)。

This approach would lead to creation of three fact records for your order 这种方法将导致为您的订单创建三个事实记录

Ordered Event 预定事件

with attributes 具有属性

 orderId, productId, orderDate  and sellingPrice

Delivered Event 交付事件

 orderId, DeliveryDate, 

Note that the order and delivery records are uniquely correlated with the OrderId (under simplified assumption of only one product per order). 请注意,订单和交货记录与OrderId唯一相关(简化假设每个订单只有一种产品)。

Both of those events are stored in separate fact table or in a common one - it depends on the full attribute definition in you case. 这两个事件都存储在单独的事实表中或一个公共的事实表中,这取决于您所用的完整属性定义。

The purchasing price is stored in a separate table with attributes 购买价格存储在具有属性的单独表中

productId, entryDate, validFromDate, ValidToDate, purchasingPrice

The table is filled based on your rules on the 15th of following month (entryDate) with the validity interval for the preceding month. 该表将根据您在下个月15号(entryDate)的规则以及上个月的有效期进行填充。

The crucial role of this table is to support the query with productId and validDate and return either the purchasing price or unknown . 该表的关键作用是支持具有productId和validDate的查询,并返回购买价格或未知价格。

Based on this design you may setup an access view (simple view, materialize view or other solution) providing the current state of the order 基于此设计,您可以设置访问视图 (简单视图,具体化视图或其他解决方案),以提供订单的当前状态

 orderId, productId, orderDate, sellingPrice,
 DeliveryDate, -- NULL if not delivered
 purchasingDate,
 purchasingStaus -- 1 purchased, 0 - not yet purchased
 purchasingPrice

The purchasingDate is calculated based on the delivery date based on the business rule. purchasingDate日期是根据基于业务规则的交货日期计算的。 The purchasingStatus is a result of comparison of the reporting date and purchasing Date. purchasingStatus是报告日期和购买日期比较的结果。

The purchasingPrice is either the estimation last known price of the product or the proper purchasing price. purchasingPrice产品的最后估计价格或适当的购买价格。

You may also ask why is immutability in data-warehouse design important. 您可能还会问,为什么数据仓库设计中的不变性很重要。 It is similar to transaction concept in OLTP. 它类似于OLTP中的事务概念。 In troubles you may rollback the data to some point in the past using auditing dimension and reload it again. 遇到麻烦时,您可以使用审核维度将数据回滚到过去的某个点,然后再次重新加载。 If you update this is much more complex. 如果更新,则要复杂得多。

Small Example 小例子

On 15.2. 15.2。 you get purchasing price for product A in January 您在一月份获得产品A的购买价格

Purchasing Price Table 采购价格表

 entryDate = 15.2.
 validFrom = 1.1.
 validTo = 31.1.
 purchacingPrice = 100

Order of product A on 1.3. 产品A在1.3上的订购。 creates a record in Order Event Table 订单事件表中创建一条记录

 orderDate = 1.3.
 sellingPrice = 200
 ...

You may report this event with "last know purchacing price", which is currently 100. (Lookup in Purchasing Price Table with orderDate gives no valid result, last stored value is returned) 您可以使用“最后知道的购买价格”(当前为100)来报告此事件。(具有orderDate的购买价格表中的查找未提供有效结果,将返回最后存储的值)

Delivery on 10.3. 10.3。交货 creates a record in Delivery Event Table 传递事件表中创建一条记录

 deliveryDate = 10.3.
 ....

The exact purchasing price is still unknown (Lookup in Purchasing Price Table with deliveryDate gives no valid result, last stored value is returned) 确切的采购价格仍然未知(带有deliveryDate的“采购价格表”中的查询未提供有效结果,将返回最后的储值)

On 15.4. 在15.4。 new purchasing price is entered in Purchasing Price Table for March. 3月的采购价格表中输入了新的采购价格。 From this points the real purchacing price is known. 从这一点上来说,实际的购买价格是已知的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM