Commit 60cb0315 authored by Jeffrey Schlitt's avatar Jeffrey Schlitt

added regression and firmed up active catalog items

parent 89bc0287
build/html/_images/RFMCatalog.png

12.9 KB | W: | H:

build/html/_images/RFMCatalog.png

21.2 KB | W: | H:

build/html/_images/RFMCatalog.png
build/html/_images/RFMCatalog.png
build/html/_images/RFMCatalog.png
build/html/_images/RFMCatalog.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -2,7 +2,7 @@
.. |catalogicon| image:: ../../images/CatalogIcon.png
:target: `Aginity Catalog Assets`_
:target: `RFM Sample Aginity Catalog Assets`_
Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments
......@@ -63,8 +63,8 @@ TRANSACTION_DETAIL
| | purchased. | |
+------------------+---------------------------------+----------------------+
Sample Data Scripts
++++++++++++++++++++
Sample RFM Data Scripts
+++++++++++++++++++++++++
The following SQL Scripts are portable and used to simulate the RFM model.
......@@ -220,17 +220,16 @@ Breakdown of the SQL
For anchor, you may define "short" anchor names like this:
Aginity Catalog Assets
RFM Sample Aginity Catalog Assets
======================================
There are three assets you can add to your catalog. I chose to add them as shown below.
There are six assets you can add to your catalog. I chose to add them as shown below.
.. image:: ../../images/RFMCatalog.png
These queries work for sure against any RedShift database and with minimal modification can work pretty much on any other supported database platform.
These queries are written using ANSII standard SQL so should work across most database platforms. Just select a connection in the Pro/Team Editor and either double click the catalog item and execute or drag and drop the catalog item which will expose the code and run them.
TRANSACTION_HEADER Asset
DATA-transaction_header
+++++++++++++++++++++++++
.. code-block:: postgresql
:linenos:
......@@ -260,7 +259,7 @@ TRANSACTION_HEADER Asset
union all
select cast(234567 as integer) as customer_id, cast(21118 as integer) as transaction_id, cast('2019-01-17' as date) as transaction_date)
TRANSACTION_DETAIL Asset
DATA-transaction_detail
+++++++++++++++++++++++++++
.. code-block:: postgresql
:linenos:
......@@ -298,41 +297,74 @@ TRANSACTION_DETAIL Asset
select cast(21118 as integer) as transaction_id, cast(1 as integer) as quantity, cast(43.10 as decimal(13,2)) as net_amount)
RFM - Sample Query Assets
+++++++++++++++++++++++++++
FML - Days Since Last Transaction (Recency)
+++++++++++++++++++++++++++++++++++++++++++++
.. code-block:: postgresql
This asset is a reusable formula that calculates the days between execution run time (current date) and the maximum transaction date for each customer.
.. code-block:: SQL
:linenos:
current_date - max(th.transaction_date)
FML - Total Spend in Period (Monetary)
++++++++++++++++++++++++++++++++++++++++
This asset is a reusable formula that calculates the aggregation (sum) of net sales, defined as quantity multiplied by net_amount over the specified time period.
.. code-block:: SQL
:linenos:
sum(td.quantity * td.net_amount)
FML - Trips per Period (Frequency)
+++++++++++++++++++++++++++++++++++++
This asset is a reusable formula that counts the number of distinct transactions within the specified time period.
.. code-block:: SQL
:linenos:
count(distinct th.transaction_id)
Sample - RFM Query
++++++++++++++++++++++++
This asset uses the formulas above and then calculates the segementation using the windowed analytic function ntile.
.. code-block:: postresql
:linenos:
with customer_metrics as
(
select
th.customer_id,
count(distinct th.transaction_id) as trips_per_period,
sum(td.quantity * td.net_amount) as total_spend_per_period,
datediff(DAY,current_date, max(th.transaction_date)) AS days_since_last_transaction
from @{/Users/jschlitt/Generic Data/TRANSACTION_HEADER} th
join (SELECT * FROM @{/Users/jschlitt/Generic Data/TRANSACTION_DETAIL}) td on th.transaction_id = td.transaction_id
where th.transaction_date > dateadd(day, cast ($lookback_days as integer)*-1,current_date)
AND td.quantity > 0 -- returns ignored
group by th.customer_id),
rfm as
(
select customer_id,
ntile($buckets) over (order by days_since_last_transaction desc) as r,
ntile($buckets) over (order by trips_per_period desc) as f,
ntile($buckets) over (order by total_spend_per_period desc) as m,
trips_per_period,
total_spend_per_period,
days_since_last_transaction
from customer_metrics
)
select customer_id
, r
, f
, m
, trips_per_period
, total_spend_per_period
, days_since_last_transaction
, ntile($buckets) over (order by 1.0*r+1.0*f+1.0*m)
from rfm;
with customer_metrics as
(
select
th.customer_id,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Trips per Period (Frequency)} as trips_per_period,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Total Spend in Period (Monetary)} as total_spend_per_period,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Days Since Last Transaction (Recency)} AS days_since_last_transaction
from @{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/DATA-transaction_header}
join (SELECT * FROM @{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/DATA-transaction_detail}) td
on th.transaction_id = td.transaction_id
where td.quantity > 0 -- returns ignored
--and th.transaction_date > dateadd(day, cast($lookback_days as integer)*-1,current_date) -- consider the past 365 days - customization opportunity (1)
group by th.customer_id),
rfm as
(
select customer_id,
ntile($buckets) over (order by days_since_last_transaction desc) as r, -- split into 10 bins - customization opportunity (2)
ntile($buckets) over (order by trips_per_period desc) as f,
ntile($buckets) over (order by total_spend_per_period desc) as m,
trips_per_period,
total_spend_per_period,
days_since_last_transaction
from customer_metrics
)
select customer_id
, r
, f
, m
, trips_per_period
, total_spend_per_period
, days_since_last_transaction
, ntile($buckets) over (order by 1.0*r+1.0*f+1.0*m)
from rfm
.. _irr:
.. |catalogicon| image:: ../../images/CatalogIcon.png
:target: `Aginity Catalog Assets`_
:target: `IRR Sample Aginity Catalog Assets`_
Lesson #1: Calculate Internal Rate of Return (IRR) Directly in Redshift
**************************************************************************
......@@ -470,7 +470,7 @@ So, one additional filter gives the final query:
.. image:: ../../images/result_6.png
Aginity Catalog Assets
IRR Sample Aginity Catalog Assets
======================================
......
This diff is collapsed.
......@@ -91,14 +91,14 @@
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -106,6 +106,7 @@
<li class="toctree-l3"><a class="reference internal" href="regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
......@@ -220,14 +221,14 @@
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l2"><a class="reference internal" href="irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l2"><a class="reference internal" href="RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l2"><a class="reference internal" href="RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l2"><a class="reference internal" href="RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -235,6 +236,7 @@
<li class="toctree-l2"><a class="reference internal" href="regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l2"><a class="reference internal" href="regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l2"><a class="reference internal" href="regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l2"><a class="reference internal" href="regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
......
......@@ -91,14 +91,21 @@
<li class="toctree-l3"><a class="reference internal" href="#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
<li class="toctree-l3"><a class="reference internal" href="regression.html#step-1-understanding-regression">Step 1 - Understanding Regression</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="regression.html#other-things-to-consider">Other things to consider</a></li>
</ul>
</li>
</ul>
......@@ -209,7 +216,7 @@
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Internal_rate_of_return">Internal Rate of Return</a> (IRR) is a calculation frequently used to estimate an investment’s rate of return.</p>
<p>I encountered it at a large mortgage lender where IRR was one of many calculations used to determine the health of individual loans. The inputs were the mortgage principle, the previous monthly payments, and estimated monthly payments to the end of the loan period. So, there could easily be more than 300 inputs to the IRR calculation. The standard practice was to transfer a sample of borrower data from the data warehouse to the SAS server to run the IRR calculation and transfer the results back to the data warehouse.</p>
<p>The type of iterative calculation required by IRR is not traditionally done on <a class="reference external" href="https://databases.looker.com/analytical">MPP</a> data warehouse platforms. However, with a bit of advanced SQL thinking, this type of calculation can take advantage of the distributed nature of the MPP to score very large populations.</p>
<p>As with all of our lessons if you want to catalog this code in <a class="reference external" href="https://www.aginity.com/products/aginity-pro/">Aginity Pro</a> or <a class="reference external" href="https://www.aginity.com/products/aginity-team/">Aginity Team</a> click the <a class="reference internal" href="#aginity-catalog-assets"><img alt="catalogicon" src="../../_images/CatalogIcon.png" /></a> to skip right to the queries to add to your catalog.</p>
<p>As with all of our lessons if you want to catalog this code in <a class="reference external" href="https://www.aginity.com/products/aginity-pro/">Aginity Pro</a> or <a class="reference external" href="https://www.aginity.com/products/aginity-team/">Aginity Team</a> click the <a class="reference internal" href="#irr-sample-aginity-catalog-assets"><img alt="catalogicon" src="../../_images/CatalogIcon.png" /></a> to skip right to the queries to add to your catalog.</p>
<div class="section" id="step-1-introduction-to-irr">
<h2>Step 1 - Introduction to IRR<a class="headerlink" href="#step-1-introduction-to-irr" title="Permalink to this headline"></a></h2>
<div class="math notranslate nohighlight">
......@@ -594,8 +601,8 @@
</div>
<img alt="../../_images/result_6.png" src="../../_images/result_6.png" />
</div>
<div class="section" id="aginity-catalog-assets">
<h2>Aginity Catalog Assets<a class="headerlink" href="#aginity-catalog-assets" title="Permalink to this headline"></a></h2>
<div class="section" id="irr-sample-aginity-catalog-assets">
<h2>IRR Sample Aginity Catalog Assets<a class="headerlink" href="#irr-sample-aginity-catalog-assets" title="Permalink to this headline"></a></h2>
</div>
<div class="section" id="additional-resources">
<h2>Additional Resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline"></a></h2>
......
This diff is collapsed.
......@@ -38,7 +38,7 @@
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata" href="redshift_utilities.html" />
<link rel="prev" title="Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments" href="../analysts/RFM.html" />
<link rel="prev" title="Lesson #6: Identify Trends Using Linear Regression" href="../analysts/regression.html" />
</head>
<body class="wy-body-for-nav">
......@@ -91,24 +91,32 @@
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../analysts/RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../analysts/RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../analysts/regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../analysts/regression.html#step-1-understanding-regression">Step 1 - Understanding Regression</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="../analysts/regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Academy for Data Engineers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......@@ -193,7 +201,7 @@
<a href="redshift_utilities.html" class="btn btn-neutral float-right" title="Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="../analysts/RFM.html" class="btn btn-neutral float-left" title="Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
<a href="../analysts/regression.html" class="btn btn-neutral float-left" title="Lesson #6: Identify Trends Using Linear Regression" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
......@@ -208,10 +216,10 @@
<p class="caption"><span class="caption-text">SQL Academy for Data Engineers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l2"><a class="reference internal" href="redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......
......@@ -90,14 +90,14 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -105,16 +105,17 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="docs/engineer/index.html">Academy for Data Engineers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......
......@@ -90,14 +90,14 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -105,16 +105,17 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="docs/engineer/index.html">Academy for Data Engineers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......@@ -218,14 +219,14 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -233,16 +234,17 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="docs/engineer/index.html">Academy for Data Engineers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......
......@@ -90,14 +90,14 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-3-irr-baby-steps">Step 3 - IRR Baby Steps</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-4-longer-strides">Step 4 - Longer Strides</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#step-5-crossing-the-finish-line">Step 5 - Crossing the Finish Line</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#irr-sample-aginity-catalog-assets">IRR Sample Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/irr_internal_rate_of_return.html#additional-resources">Additional Resources</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/RFM.html">Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-1-the-required-data">Step 1 - The Required Data</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#step-2-the-rfm-query">Step 2 - The RFM Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/RFM.html#rfm-sample-aginity-catalog-assets">RFM Sample Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/analysts/regression.html">Lesson #6: Identify Trends Using Linear Regression</a><ul>
......@@ -105,16 +105,17 @@
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-2-building-it-in-sql">Step 2 - Building it in SQL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#step-3-taking-it-to-the-next-step">Step 3 - Taking it to the Next Step</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#other-things-to-consider">Other things to consider</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/analysts/regression.html#sample-regression-aginity-catalog-assets">Sample Regression Aginity Catalog Assets</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="docs/engineer/index.html">Academy for Data Engineers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/redshift_utilities.html">Lesson #2: Essential Redshift Utilities: Generate DDL and Search Table Metadata</a><ul>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-generate-ddl">Step 1 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-search-table-metadata">Step 2 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-don-t-forget-the-discovery-tab">Step 3 - Don’t Forget the Discovery Tab</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#aginity-catalog-assets">Aginity Catalog Assets</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-1-search-table-metadata">Step 1 - Search Table Metadata</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-2-generate-drop-table-query">Step 2 - Generate Drop Table Query</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#step-3-generate-ddl">Step 3 - Generate DDL</a></li>
<li class="toctree-l3"><a class="reference internal" href="docs/engineer/redshift_utilities.html#redshift-utilities-aginity-catalog-assets">Redshift Utilities Aginity Catalog Assets</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="docs/engineer/generate_data_profile_sql.html">Lesson #3: Generating SQL to Profile Table Data</a><ul>
......
This diff is collapsed.
......@@ -2,7 +2,7 @@
.. |catalogicon| image:: ../../images/CatalogIcon.png
:target: `Aginity Catalog Assets`_
:target: `RFM Sample Aginity Catalog Assets`_
Lesson #5: Learn to Create Basic Recency, Frequency and Monetary Segments
......@@ -63,8 +63,8 @@ TRANSACTION_DETAIL
| | purchased. | |
+------------------+---------------------------------+----------------------+
Sample Data Scripts
++++++++++++++++++++
Sample RFM Data Scripts
+++++++++++++++++++++++++
The following SQL Scripts are portable and used to simulate the RFM model.
......@@ -220,17 +220,16 @@ Breakdown of the SQL
For anchor, you may define "short" anchor names like this:
Aginity Catalog Assets
RFM Sample Aginity Catalog Assets
======================================
There are three assets you can add to your catalog. I chose to add them as shown below.
There are six assets you can add to your catalog. I chose to add them as shown below.
.. image:: ../../images/RFMCatalog.png
These queries work for sure against any RedShift database and with minimal modification can work pretty much on any other supported database platform.
These queries are written using ANSII standard SQL so should work across most database platforms. Just select a connection in the Pro/Team Editor and either double click the catalog item and execute or drag and drop the catalog item which will expose the code and run them.
TRANSACTION_HEADER Asset
DATA-transaction_header
+++++++++++++++++++++++++
.. code-block:: postgresql
:linenos:
......@@ -260,7 +259,7 @@ TRANSACTION_HEADER Asset
union all
select cast(234567 as integer) as customer_id, cast(21118 as integer) as transaction_id, cast('2019-01-17' as date) as transaction_date)
TRANSACTION_DETAIL Asset
DATA-transaction_detail
+++++++++++++++++++++++++++
.. code-block:: postgresql
:linenos:
......@@ -298,41 +297,74 @@ TRANSACTION_DETAIL Asset
select cast(21118 as integer) as transaction_id, cast(1 as integer) as quantity, cast(43.10 as decimal(13,2)) as net_amount)
RFM - Sample Query Assets
+++++++++++++++++++++++++++
FML - Days Since Last Transaction (Recency)
+++++++++++++++++++++++++++++++++++++++++++++
.. code-block:: postgresql
This asset is a reusable formula that calculates the days between execution run time (current date) and the maximum transaction date for each customer.
.. code-block:: SQL
:linenos:
current_date - max(th.transaction_date)
FML - Total Spend in Period (Monetary)
++++++++++++++++++++++++++++++++++++++++
This asset is a reusable formula that calculates the aggregation (sum) of net sales, defined as quantity multiplied by net_amount over the specified time period.
.. code-block:: SQL
:linenos:
sum(td.quantity * td.net_amount)
FML - Trips per Period (Frequency)
+++++++++++++++++++++++++++++++++++++
This asset is a reusable formula that counts the number of distinct transactions within the specified time period.
.. code-block:: SQL
:linenos:
count(distinct th.transaction_id)
Sample - RFM Query
++++++++++++++++++++++++
This asset uses the formulas above and then calculates the segementation using the windowed analytic function ntile.
.. code-block:: postresql
:linenos:
with customer_metrics as
(
select
th.customer_id,
count(distinct th.transaction_id) as trips_per_period,
sum(td.quantity * td.net_amount) as total_spend_per_period,
datediff(DAY,current_date, max(th.transaction_date)) AS days_since_last_transaction
from @{/Users/jschlitt/Generic Data/TRANSACTION_HEADER} th
join (SELECT * FROM @{/Users/jschlitt/Generic Data/TRANSACTION_DETAIL}) td on th.transaction_id = td.transaction_id
where th.transaction_date > dateadd(day, cast ($lookback_days as integer)*-1,current_date)
AND td.quantity > 0 -- returns ignored
group by th.customer_id),
rfm as
(
select customer_id,
ntile($buckets) over (order by days_since_last_transaction desc) as r,
ntile($buckets) over (order by trips_per_period desc) as f,
ntile($buckets) over (order by total_spend_per_period desc) as m,
trips_per_period,
total_spend_per_period,
days_since_last_transaction
from customer_metrics
)
select customer_id
, r
, f
, m
, trips_per_period
, total_spend_per_period
, days_since_last_transaction
, ntile($buckets) over (order by 1.0*r+1.0*f+1.0*m)
from rfm;
with customer_metrics as
(
select
th.customer_id,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Trips per Period (Frequency)} as trips_per_period,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Total Spend in Period (Monetary)} as total_spend_per_period,
@{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/FML - Days Since Last Transaction (Recency)} AS days_since_last_transaction
from @{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/DATA-transaction_header}
join (SELECT * FROM @{/Samples/Sample Data Science Queries - All Platforms/Recency-Frequency-Monetary Samples/DATA-transaction_detail}) td
on th.transaction_id = td.transaction_id
where td.quantity > 0 -- returns ignored
--and th.transaction_date > dateadd(day, cast($lookback_days as integer)*-1,current_date) -- consider the past 365 days - customization opportunity (1)
group by th.customer_id),
rfm as
(
select customer_id,
ntile($buckets) over (order by days_since_last_transaction desc) as r, -- split into 10 bins - customization opportunity (2)
ntile($buckets) over (order by trips_per_period desc) as f,
ntile($buckets) over (order by total_spend_per_period desc) as m,
trips_per_period,
total_spend_per_period,
days_since_last_transaction
from customer_metrics
)
select customer_id
, r
, f
, m
, trips_per_period
, total_spend_per_period
, days_since_last_transaction
, ntile($buckets) over (order by 1.0*r+1.0*f+1.0*m)
from rfm
.. _irr:
.. |catalogicon| image:: ../../images/CatalogIcon.png
:target: `Aginity Catalog Assets`_