Behavior Tag Time Series は、顧客に期(年・四半期・月など)ごとに付けた行動タグの並びを、顧客ディメンションテーブルに期ごとの列として持つ設計テクニックである。行動タグは、購買行動が近い顧客をまとめた区分の名前で、値は数値ではなく文字列である。 この手法は [[ディメンショナルモデリング]] の設計テクニックの 1 つで、[[Ralph Kimball]] が 2002 年の記事で示した。1 人の顧客の並びを 1 行に収めるため、「優良だった顧客の返品が増えた」のようなタグの移り変わりを、`where` 句の条件だけで絞り込める。 > [!info] > 原典は次の 3 つである。 > > - 『[[The Data Warehouse Toolkit]]』(3rd Edition) 8 章 "Customer Relationship Management" の "Behavior Tag Time Series" > - Kimball Group の [Behavior Tag Time Series](https://www.kimballgroup.com/data-warehouse-business-intelligence-resources/kimball-techniques/dimensional-modeling-techniques/behavior-tag-series-attribute/) テクニック > - Ralph Kimball の記事 [Wrangling Behavior Tags](https://web.archive.org/web/20031003234831/http://www.intelligententerprise.com:80/020509/508warehouse1_1.shtml) (Intelligent Enterprise、2002 年 5 月) ## 構造 ### 行動タグ 行動タグは、顧客を採点し、点数が近い顧客をまとめた区分に付ける名前である。Kimball の例では、RFM 分析に近い 3 つの点数 (最後の注文からの日数、注文の回数、使った金額) で採点し、クラスター分析でまとめた区分に A〜H を付けている。このタグは、信用と返品の状況も加味している。 | 行動タグ | 顧客の特徴 | | --- | --- | | A | 購入が多いリピーター。信用は良好で、返品は少ない | | B | 購入が多いリピーター。信用は良好だが、返品が多い | | C | 最近の新規顧客。信用の実績はまだ無い | | D | ときどき購入する顧客。信用は良好 | | E | ときどき購入する顧客。信用は低い | | F | 以前は優良だったが、最近は来ていない | | G | よく見に来るが、ほとんど買わない | | H | その他 | 以降は、年が終わるたびにこのタグを 1 つ付ける例で解説する。8 人の顧客に、5 年間で次のタグが付いたとする。 | 顧客 | 5 年前 | 4 年前 | 3 年前 | 2 年前 | 1 年前 | | --- | --- | --- | --- | --- | --- | | Alex | C | D | A | A | B | | Bell | | C | A | A | F | | Cassy | | | C | A | A | | David | C | A | F | D | A | | Emma | C | A | B | A | A | | Frank | C | A | F | | | | Grace | | C | G | G | G | | Henry | | | C | A | B | 空欄は、その年に会員ではなかったことを表す。Cassy は 3 年前に加入し、Frank は 2 年前から会員ではない。Bell は退会していないが、1 年前のタグは F になっている。 > [!note] > この例では、期を「何年前」で数える。新しい年のタグが出るたびに全員の値が 1 つ古い期へ移るが、クエリは年を書き換えずに使い続けられる。 > > Kimball の 2002 年の記事は、列を暦の期に固定している。記事は月次の 24 列を例にして、使い切ったら列を足すと述べている。 > > 期の長さと数は、分析で遡りたい長さから決める。 ### 顧客ディメンションテーブル `dim_user` は、この並びを顧客 1 人につき 1 行で持つ。「何年前のタグか」を表す列を 5 つ持ち、5 年分のタグを古い順に連結した `customer_segment_series` 列も持つ。 ```mermaid erDiagram dim_user ||--|{ fct_order : "user_key" fct_order["fct_order [TF]"] { order_key int64 "[SK]" user_key int64 "[FK]" order_id string "[GD, DD]" order_line_number int64 "[GD, DD]" order_amount int64 "[FA]" } dim_user { user_key int64 "[SK]" user_id string "[NK]" user_name string customer_segment_5y_ago string customer_segment_4y_ago string customer_segment_3y_ago string customer_segment_2y_ago string customer_segment_1y_ago string customer_segment_series string } ``` ``` dim_user | user_key (SK) | user_id (NK) | user_name | customer_segment_5y_ago | customer_segment_4y_ago | customer_segment_3y_ago | customer_segment_2y_ago | customer_segment_1y_ago | customer_segment_series | | ------------- | ------------ | --------- | ----------------------- | ----------------------- | ----------------------- | ----------------------- | ----------------------- | ----------------------- | | 1 | usr-001 | Alex | C | D | A | A | B | CDAAB | | 2 | usr-002 | Bell | (Not Applicable) | C | A | A | F | -CAAF | | 3 | usr-003 | Cassy | (Not Applicable) | (Not Applicable) | C | A | A | --CAA | | 4 | usr-004 | David | C | A | F | D | A | CAFDA | | 5 | usr-005 | Emma | C | A | B | A | A | CABAA | | 6 | usr-006 | Frank | C | A | F | (Not Applicable) | (Not Applicable) | CAF-- | | 7 | usr-007 | Grace | (Not Applicable) | C | G | G | G | -CGGG | | 8 | usr-008 | Henry | (Not Applicable) | (Not Applicable) | C | A | B | --CAB | ``` > [!note] > `(Not Applicable)` は、会員ではなかった年に入れたデフォルト値で、連結した列では `-` の 1 文字にしている。この 2 つの値は原典には無い。ラベルの語彙は [[Default Row]] の属性に入れるものと揃えている。 > > 原典は列名を示していないため、本ノートでは区分の中身が分かる名前を付けている。この例の `customer_segment` は、タグ A〜H に付けた名前である。 ## 使い方 ### タグの移り変わりで絞り込む 2 年前は A で、1 年前に B になった顧客を探す。A と B はどちらも購入が多い顧客で、B は返品が多い。条件にする 2 つの列は同じ行にあるため、`where` 句に並べるだけで書ける。 ```sql select user_name, customer_segment_series, from dim_user where customer_segment_2y_ago = 'A' and customer_segment_1y_ago = 'B' ``` ``` | user_name | customer_segment_series | | --------- | ----------------------- | | Alex | CDAAB | | Henry | --CAB | ``` ### 順序をワイルドカードで書く 連結した列には `like` 演算子を使える。`%` は 0 文字以上の文字列に一致する。次のクエリは、F だった年より後に、A だった年がある顧客を探す。 ```sql select user_name, customer_segment_series, from dim_user where customer_segment_series like '%F%A%' ``` ``` | user_name | customer_segment_series | | --------- | ----------------------- | | David | CAFDA | ``` この条件を期ごとの列で書くと、F の年と A の年の組み合わせを `or` ですべて並べることになる。連結した列は、8 章が期ごとの列に加えて持つよう勧めているものである。 パターンを変えると、ほかの移り変わりも同じ形で絞り込める。 | パターン | 一致する顧客 | 結果 | | --- | --- | --- | | `'%AB'` | 2 年前が A で、1 年前が B | Alex、Henry | | `'%A%B%'` | A だった年より後に、B だった年がある | Alex、Emma、Henry | | `'%F'` | 1 年前は F だった | Bell | | `'%-'` | 1 年前は会員ではなかった | Frank | > [!warning] > 連結した列を検索するには、タグを 1 文字で付ける。長さが揃っていないタグを連結すると、文字の位置と年が対応しなくなる。1 文字にしておけば、連結した列の 1 文字が 1 年に対応する。 ## ミニディメンションとの併用 年齢帯や年収帯のように変化が多い属性は、変わるたびに `dim_user` へ行を足して履歴を持つと、行が増え続ける。 ミニディメンションは、このように変化が多い属性を顧客ディメンションテーブルから切り出した、小さなディメンションテーブルである。属性の値の組み合わせを 1 行として持ち、[[ファクトテーブル]] が外部キーで参照する。 Kimball は 8 章で、行動タグをミニディメンションにも入れ、期ごとの列と併用するよう勧めている。期ごとの列は顧客の絞り込みに使い、ミニディメンションは注文した時点のタグでの集計に使う。次の例では、5 つの属性を `dim_user_demographic` にまとめている。 ```mermaid erDiagram dim_user ||--|{ fct_order : "user_key" dim_user_demographic ||--|{ fct_order : "user_demographic_key" dim_user_demographic ||--|{ dim_user : "current_user_demographic_key" fct_order["fct_order [TF]"] { order_key int64 "[SK]" user_key int64 "[FK]" user_demographic_key int64 "[FK]" order_id string "[GD, DD]" order_line_number int64 "[GD, DD]" order_amount int64 "[FA]" } dim_user { user_key int64 "[SK]" user_id string "[NK]" current_user_demographic_key int64 "[FK]" user_name string gender string birth_date date registered_date date prefecture string annual_income int64 household_size int64 marital_status string customer_segment_5y_ago string customer_segment_4y_ago string customer_segment_3y_ago string customer_segment_2y_ago string customer_segment_1y_ago string customer_segment_series string } dim_user_demographic { user_demographic_key int64 "[SK]" age_band string "[ND1]" income_band string "[ND1]" household_size_band string "[ND1]" marital_status string "[ND1]" customer_segment string "[ND1]" } ``` > [!note] > `dim_user` の `current_user_demographic_key` は、顧客の現在の属性を持つ行を指す。ミニディメンションは、注文した時点の属性と現在の属性という 2 つの役割で参照される。 > > ミニディメンションには、変化が多い属性をまとめて入れる。Kimball は 10 章 "Financial Services" で、属性ごとにミニディメンションを作ると、ファクトテーブルの外部キーが増えすぎると述べている。