Fivetran x Shopify: discount の扱い (の現状の理解)

多分こういうことだと理解 まだ調べ中なのであとで訂正するかも

前提: Shopify のデータを Fivetran で BigQuery に ETL

リンク集

discount_allocation について

The discount_allocation object associates a discount_application with a line item.

discount_application について

The discount_application object registers discounts at the cart, checkout, or order level.

...と書いてあるが、Fivetran の schema を見ると order_id が入っているので、 discount_application テーブルに入ってくるのは order level のものだと思われる

つまり discount_allocation は order line item (注文の中の商品1個1個) 単位での割引適用で、 discount_application は注文単位の割引適用ということらしい

FK は多分こんな感じ

2023-12-30 追記: discount_application.indexdiscount_allocation.discount_application_index で join できるかと思ってたけど違った。1) Shopify の API docs で index について読むと「割引の適用順を示すものですよ」とあったのと、2) Fivetran のサポートに join のクエリを教えてもらった。

select * 
from `lake_shopify.discount_application` as dap 
  join `lake_shopify.order_line` as ol
    on dap.order_id=ol.order_id 
  join `lake_shopify.discount_allocation` dal on ol.id=dal.order_line_id

sample queries

顧客ごとのディスカウントされた注文回数

select customer_id, count(*) as num_discounted_orders
from `lake_shopify.discount_application` as ap
  join `lake_shopify.order` as o on o.id = ap.order_id
-- do not include orders that are archived, not paid, not shipped, etc.
where o.financial_status = "paid" and o.fulfillment_status = "fulfilled"
group by 1