多分こういうことだと理解 まだ調べ中なのであとで訂正するかも
前提: Shopify のデータを Fivetran で BigQuery に ETL
リンク集
- テーマファイルで discount を扱う方法: https://shopify.dev/docs/themes/pricing-payments/discounts
- Admin REST API: https://shopify.dev/docs/themes/pricing-payments/discounts (多分 Fivetran はここからひっぱってる)
- Fivetran schema: https://docs.google.com/presentation/d/1wSWI7SbY4NMtyRLWdg2Z4LW3-SRCF8K7McN0VzLjh3w/edit#slide=id.g2980d8d3b7a_1_0 (※2023年12月時点のリンク。最新版はここから辿ってね)
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.index
と discount_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