forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: luhuayi <luhuayi@huawei.com> Co-committed-by: luhuayi <luhuayi@huawei.com>
258 lines
30 KiB
HTML
258 lines
30 KiB
HTML
<a name="EN-US_TOPIC_0000001811515569"></a><a name="EN-US_TOPIC_0000001811515569"></a>
|
|
|
|
<h1 class="topictitle1">Funnel and Retention Functions</h1>
|
|
<div id="body0000001688448222"><p id="EN-US_TOPIC_0000001811515569__p1361405612713">Funnel and retention functions are supported only in cluster 8.3.0 and later versions.</p>
|
|
<div class="section" id="EN-US_TOPIC_0000001811515569__section1692151371515"><h4 class="sectiontitle">Context</h4><p id="EN-US_TOPIC_0000001811515569__p15977124718919">Both funnel functions and retention functions are widely used tools for analyzing user behavior in product and data analysis. They are particularly useful for product managers, data scientists, and marketing personnel. These functions assist in analyzing the customer journey, comprehending user churn and conversion problems, and assessing the product's ability to consistently attract and retain customers.</p>
|
|
<ul id="EN-US_TOPIC_0000001811515569__ul53473472532"><li id="EN-US_TOPIC_0000001811515569__li943411515109">Funnel analysis: examines each step a customer takes from initial contact to final purchase. It helps product teams and marketers understand how users interact with products or services, as well as the conversion and churn rates at each step. The funnel analysis involves designating stages of the customer's journey from first hearing about a product to making a purchase. The whole process may include visiting a website, registering an account, and purchasing a product.</li><li id="EN-US_TOPIC_0000001811515569__li2034814775319">Retention analysis: focuses on the percentage of users who continue using a product after a specified period of time. It calculates the number of users who remain active after a certain time point, such as user registration, first order placement, or initial use. Common retention analysis functions include daily, weekly, and monthly retention rates, which help analyze whether users continue using the product after initially hearing about it.</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001811515569__section12534154161110"><h4 class="sectiontitle">WINDOW_FUNNEL</h4><p id="EN-US_TOPIC_0000001811515569__p42230557275">The <strong id="EN-US_TOPIC_0000001811515569__b1339374113142">WINDOW_FUNNEL</strong> function searches for an event chain in the sliding time window and counts the maximum number of consecutive events in the event chain.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p42668341804">Given a list of user-defined events, <span id="EN-US_TOPIC_0000001811515569__text179579506176">GaussDB(DWS)</span> finds the longest sequential match starting from the first event and returns the length of the match. Once the matching fails, the entire matching ends. Example:</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1576327114710">Assume that the window is large enough:</p>
|
|
<ul id="EN-US_TOPIC_0000001811515569__ul3761927194711"><li id="EN-US_TOPIC_0000001811515569__li5761927114715">The condition events are c1, c2, and c3, but the user data is c1, c2, c3, and c4. So, c1, c2, and c3 are matched, and the return value of the function is 3.</li><li id="EN-US_TOPIC_0000001811515569__li1577132719473">The condition events are c1, c2, and c3, but the user data is c4, c3, c2, and c1. So, c1 is matched, and the return value of the function is 1.</li><li id="EN-US_TOPIC_0000001811515569__li1277152734714">The condition events are c1, c2, and c3, but the user data is c4 and c3. So, no event is matched, and the return value of the function is 0.</li></ul>
|
|
<p id="EN-US_TOPIC_0000001811515569__p131971041174716"><strong id="EN-US_TOPIC_0000001811515569__b11556219398">Function syntax</strong></p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen1046113014816"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">windowFunnel</span><span class="p">(</span><span class="n">window</span><span class="p">,</span><span class="w"> </span><span class="k">mode</span><span class="p">,</span><span class="w"> </span><span class="k">timestamp</span><span class="p">,</span><span class="w"> </span><span class="n">cond1</span><span class="p">,</span><span class="w"> </span><span class="n">cond2</span><span class="p">,</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">condN</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1593235310503"><strong id="EN-US_TOPIC_0000001811515569__b1047104585917">Input parameters</strong></p>
|
|
<ul id="EN-US_TOPIC_0000001811515569__ul17849952522"><li id="EN-US_TOPIC_0000001811515569__li1184912535211"><strong id="EN-US_TOPIC_0000001811515569__b04567146417">window</strong>: bigint type. It indicates the size of the sliding time window. The unit is second.</li><li id="EN-US_TOPIC_0000001811515569__li19849357525"><strong id="EN-US_TOPIC_0000001811515569__b1352519179439">mode</strong>: text type. Currently, only the <strong id="EN-US_TOPIC_0000001811515569__b249282520457">Default</strong> mode is supported. For other modes, an error is reported. In the <strong id="EN-US_TOPIC_0000001811515569__b64102311474">Default</strong> mode, it matches as many events as possible, starting from the first event in the window.</li><li id="EN-US_TOPIC_0000001811515569__li118491156525"><strong id="EN-US_TOPIC_0000001811515569__b186849293479">timestamp</strong>: time range when an event occurs. The <strong id="EN-US_TOPIC_0000001811515569__b128368459475">timestamp without time zone</strong>, <strong id="EN-US_TOPIC_0000001811515569__b1311724912475">timestamp with time zone</strong>, <strong id="EN-US_TOPIC_0000001811515569__b174595518478">date</strong>, <strong id="EN-US_TOPIC_0000001811515569__b15878165313471">int</strong> and<strong id="EN-US_TOPIC_0000001811515569__b12116570477"> bigint</strong> types are supported.</li><li id="EN-US_TOPIC_0000001811515569__li10849135115217"><strong id="EN-US_TOPIC_0000001811515569__b1415876164813">cond</strong>: variable-length Boolean array. It indicates the condition. <span id="EN-US_TOPIC_0000001811515569__text2682122935919">GaussDB(DWS)</span> supports only 1 to 32 conditions. If the number of conditions is not within the range, an error is reported.</li></ul>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1562778203220"><strong id="EN-US_TOPIC_0000001811515569__b1825516434592">Return values</strong></p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p105681740105910"><strong id="EN-US_TOPIC_0000001811515569__b196132102051">level</strong>: int type. It indicates the maximum length of the event list that matches the condition.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001811515569__section125741431132511"><h4 class="sectiontitle">Retention Functions</h4><p id="EN-US_TOPIC_0000001811515569__p10563716478">The retention function evaluates if an event meets each condition, starting from the first one.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p491311019520"><strong id="EN-US_TOPIC_0000001811515569__b11814015143918">Function syntax</strong></p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen036010101376"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">retention</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span><span class="w"> </span><span class="n">cond2</span><span class="p">,</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">cond32</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001811515569__p108792087511"><strong id="EN-US_TOPIC_0000001811515569__b15828163671">Input parameters</strong></p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p22718171072"><strong id="EN-US_TOPIC_0000001811515569__b47993529916">cond</strong>: variable-length Boolean array with a maximum length of 32 characters, indicating whether an event meets specific conditions. <span id="EN-US_TOPIC_0000001811515569__text16687151814416">GaussDB(DWS)</span> supports only 1 to 32 conditions. If the number of conditions is not within the range, an error is reported.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p156707511966"><strong id="EN-US_TOPIC_0000001811515569__b1342100475">Return values</strong></p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p15210995239"><strong id="EN-US_TOPIC_0000001811515569__b1184014324619">retention condition</strong>: tinyint array type. It indicates the expression of the returned result, which is the same as the length of the input parameter <strong id="EN-US_TOPIC_0000001811515569__b159526578551">cond</strong>. If the <strong id="EN-US_TOPIC_0000001811515569__b9694318125617">cond1</strong> and <strong id="EN-US_TOPIC_0000001811515569__b587052114566">condi</strong> conditions are met, the ith value of the returned result is <strong id="EN-US_TOPIC_0000001811515569__b1724963235611">1</strong>. Otherwise, the ith value is <strong id="EN-US_TOPIC_0000001811515569__b9886123465616">0</strong>.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001811515569__section845415725119"><h4 class="sectiontitle">Other Retention-Related Functions</h4><p id="EN-US_TOPIC_0000001811515569__p153021843132819">GaussDB(DWS) supports functions <strong id="EN-US_TOPIC_0000001811515569__b2898113519169">range_retention_count</strong> and <strong id="EN-US_TOPIC_0000001811515569__b12801193817164">range_retention_sum</strong> as supplements to the retention function for better customer retention analysis.</p>
|
|
<ul id="EN-US_TOPIC_0000001811515569__ul412214113292"><li id="EN-US_TOPIC_0000001811515569__li3122018295"><strong id="EN-US_TOPIC_0000001811515569__b5102336122717">range_retention_count</strong><p id="EN-US_TOPIC_0000001811515569__p1918112108518"><strong id="EN-US_TOPIC_0000001811515569__b11994133771812">range_retention_count</strong> calculates the retention rate of each user. This function returns an array, which can be used as the input parameter of the <strong id="EN-US_TOPIC_0000001811515569__b207836515185">range_retention_sum</strong> function.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p19352142419541"><strong id="EN-US_TOPIC_0000001811515569__b356142123915">Function syntax</strong></p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen830263465419"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">range_retention_count</span><span class="p">(</span><span class="n">is_first</span><span class="p">,</span><span class="w"> </span><span class="n">is_active</span><span class="p">,</span><span class="w"> </span><span class="n">dt</span><span class="p">,</span><span class="w"> </span><span class="n">retention_interval</span><span class="p">,</span><span class="w"> </span><span class="n">retention_granularity</span><span class="p">,</span><span class="w"> </span><span class="n">output_format</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001811515569__p119471327145411"><strong id="EN-US_TOPIC_0000001811515569__b3252124885614">Input parameters</strong></p>
|
|
<ul id="EN-US_TOPIC_0000001811515569__ul1830910543567"><li id="EN-US_TOPIC_0000001811515569__li11309954175619"><strong id="EN-US_TOPIC_0000001811515569__b14186115215613">is_first</strong>: indicates whether it is an initial behavior. The value is of the Boolean type. The value <strong id="EN-US_TOPIC_0000001811515569__b1471311101576">true</strong> indicates that it is an initial behavior, and the value <strong id="EN-US_TOPIC_0000001811515569__b420731425715">false</strong> indicates that it is not an initial behavior.</li><li id="EN-US_TOPIC_0000001811515569__li13617285720"><strong id="EN-US_TOPIC_0000001811515569__b122861854185718">is_active</strong>: indicates whether it is a retention behavior. The value is of the Boolean type. The value <strong id="EN-US_TOPIC_0000001811515569__b971242185913">true</strong> indicates that it is a retention behavior, and the value <strong id="EN-US_TOPIC_0000001811515569__b16881115685910">false</strong> indicates that it is not a retention behavior.</li><li id="EN-US_TOPIC_0000001811515569__li175231611125713"><strong id="EN-US_TOPIC_0000001811515569__b11352135916014">dt</strong>: indicates the date when the behavior happens. The value is of the date type.</li><li id="EN-US_TOPIC_0000001811515569__li159551218155711"><strong id="EN-US_TOPIC_0000001811515569__b149981845757">retention_interval</strong>: indicates the storage interval. The value is of the array type. A maximum of 15 storage intervals are supported. For example, ARRAY[1,3,5,7,15,30].</li><li id="EN-US_TOPIC_0000001811515569__li4333183314570"><strong id="EN-US_TOPIC_0000001811515569__b17536686614">retention_granularity</strong>: indicates the retention granularity, which can be day, week, or month. The value is of the text type.</li><li id="EN-US_TOPIC_0000001811515569__li378223919574"><strong id="EN-US_TOPIC_0000001811515569__b795118369611">output_format</strong>: indicates the output format. The value is of the text type and can be <strong id="EN-US_TOPIC_0000001811515569__b117761501669">normal</strong> (default) or <strong id="EN-US_TOPIC_0000001811515569__b16538135418615">expand</strong> (daily retention details can be obtained).</li></ul>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1179813416563"><strong id="EN-US_TOPIC_0000001811515569__b138057141883">Return value</strong>: BIGINT array of user retention information.</p>
|
|
</li><li id="EN-US_TOPIC_0000001811515569__li36991226142911"><strong id="EN-US_TOPIC_0000001811515569__b1513314436298">range_retention_sum</strong><p id="EN-US_TOPIC_0000001811515569__p167381535111314"><strong id="EN-US_TOPIC_0000001811515569__b7269183511919">range_retention_sum</strong> summarizes and calculates the daily (weekly/monthly) retention rate of all users.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p2529113143"><strong id="EN-US_TOPIC_0000001811515569__b1856721113920">Function syntax</strong></p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen064483601413"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">range_retention_sum</span><span class="p">(</span><span class="n">range_retention_count_result</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001811515569__p479412319148"><strong id="EN-US_TOPIC_0000001811515569__b142714541084">Input parameter</strong>: return value of the <strong id="EN-US_TOPIC_0000001811515569__b85191642914">range_retention_count</strong> function.</p>
|
|
<p id="EN-US_TOPIC_0000001811515569__p17244443111413"><strong id="EN-US_TOPIC_0000001811515569__b157331813918">Return value</strong>: text array of user retention statistics.</p>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001811515569__section0101141411610"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001811515569__p12559244481">Create the <strong id="EN-US_TOPIC_0000001811515569__b1828820192016">funnel_test</strong> table.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen171352034819">CREATE TABLE IF NOT EXISTS funnel_test
|
|
(
|
|
user_id INT ,
|
|
event_type TEXT,
|
|
event_time TIMESTAMP,
|
|
event_timez TIMESTAMP WITH TIME ZONE,
|
|
event_time_int BIGINT
|
|
);</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p15606141214493">Insert data.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen43656712493">INSERT INTO funnel_test VALUES
|
|
(1,'Browse','2021-01-31 11:00:00', '2021-01-31 11:00:00+08', 10),
|
|
(1,'Click','2021-01-31 11:10:00', '2021-01-31 11:10:00+07', 20),
|
|
(1,'Order','2021-01-31 11:20:00', '2021-01-31 11:20:00+06', 30),
|
|
(1,'Pay','2021-01-31 11:30:00', '2021-01-31 11:30:00+05', 40),
|
|
(2,'Order','2021-01-31 11:00:00', '2021-01-31 11:00:00+08', 11),
|
|
(2,'Pay','2021-01-31 11:10:00', '2021-01-31 11:10:00+08', 12),
|
|
(1,'Browse','2021-01-31 11:00:00', '2021-01-31 11:00:00+01', 50),
|
|
(3,'Browse','2021-01-31 11:20:00', '2021-01-31 11:20:00-04', 30),
|
|
(3,'Click','2021-01-31 12:00:00', '2021-01-31 12:00:00-04', 80),
|
|
(4,'Browse','2021-01-31 11:50:00', '2021-01-31 11:50:00-01', 1000),
|
|
(4,'Pay','2021-01-31 12:00:00', '2021-01-31 12:00:00-02', 900),
|
|
(4,'Order','2021-01-31 12:00:00', '2021-01-31 12:00:00-03', 1001),
|
|
(4,'Click','2021-01-31 12:00:00', '2021-01-31 12:00:00-04', 1001),
|
|
(5,'Browse','2021-01-31 11:50:00', '2021-01-31 11:50:00+08', NULL),
|
|
(5,'Click','2021-01-31 12:00:00', '2021-01-31 12:00:00+08', 776),
|
|
(5,'Order','2021-01-31 11:10:00', '2021-01-31 11:10:00+08', 999),
|
|
(6,'Browse','2021-01-31 11:50:00', '2021-01-31 11:50:00+01', -1),
|
|
(6,'Click','2021-01-31 12:00:00', '2021-01-31 12:00:00+02', -2),
|
|
(6,'Order','2021-01-31 12:10:00', '2021-01-31 12:00:00+03', -3);</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p176368115347">Calculate the funnel for each user. In the <strong id="EN-US_TOPIC_0000001811515569__b12634442102220">level</strong> column of the following command output, <strong id="EN-US_TOPIC_0000001811515569__b82514257213">0</strong> means that there were no events in the window period and <strong id="EN-US_TOPIC_0000001811515569__b5978718234">1</strong> means that there was one event in the window period.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen1290174975516">SELECT
|
|
user_id,
|
|
windowFunnel(
|
|
0, 'default', event_timez,
|
|
event_type = 'Browse', event_type = 'Click', event_type = 'Order', event_type = 'Pay'
|
|
) AS level
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
ORDER by user_id;
|
|
|
|
user_id | level
|
|
---------+-------
|
|
1 | 1
|
|
2 | 0
|
|
3 | 1
|
|
4 | 1
|
|
5 | 1
|
|
6 | 1
|
|
(6 rows)</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1763613117342">Calculate the funnel of each user and specify the length of the sliding time window as <strong id="EN-US_TOPIC_0000001811515569__b44272052182618">NULL</strong>. An error is reported.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen1845419410011">SELECT
|
|
user_id,
|
|
windowFunnel(
|
|
NULL, 'default', event_time,
|
|
event_type = 'Browse', event_type = 'Click', event_type = 'Order', event_type = 'Pay'
|
|
) AS level
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
ORDER by user_id;
|
|
ERROR: Invalid parameter : window length or mode is null.</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p83581519362">Calculate the funnel of each user and specify multiple conditions.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen73631655275">SELECT
|
|
user_id,
|
|
windowFunnel(
|
|
40, 'default', date(event_time),
|
|
true, true, false, true
|
|
) AS level
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
ORDER by user_id;
|
|
user_id | level
|
|
---------+-------
|
|
1 | 2
|
|
2 | 2
|
|
3 | 2
|
|
4 | 2
|
|
5 | 2
|
|
6 | 2
|
|
(6 rows)</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p153615152369">Check the retention rate of each user in each event.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen12211121911913">SELECT
|
|
user_id,
|
|
retention(
|
|
event_type = 'Browse', event_type = 'Click', event_type = 'Order', event_type = 'Pay'
|
|
) AS r
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
ORDER BY user_id ASC;
|
|
user_id | r
|
|
---------+-----------
|
|
1 | {1,1,1,1}
|
|
2 | {0,0,0,0}
|
|
3 | {1,1,0,0}
|
|
4 | {1,1,1,1}
|
|
5 | {1,1,1,0}
|
|
6 | {1,1,1,0}
|
|
(6 rows)</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p102183179">Analyze the retention rate of each user in each event and set the first time point to <strong id="EN-US_TOPIC_0000001811515569__b185568193286">false</strong>.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen168288215128">SELECT
|
|
user_id,
|
|
retention(
|
|
false, event_type = 'Browse', event_type = 'Click', event_type = 'Order', event_type = 'Pay'
|
|
) AS r
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
ORDER BY user_id ASC;
|
|
user_id | r
|
|
---------+-------------
|
|
1 | {0,0,0,0,0}
|
|
2 | {0,0,0,0,0}
|
|
3 | {0,0,0,0,0}
|
|
4 | {0,0,0,0,0}
|
|
5 | {0,0,0,0,0}
|
|
6 | {0,0,0,0,0}
|
|
(6 rows)</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p9146361797">Analyze the retention rate of all users in each event.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen1067982614153">SELECT sum(r[1]), sum(r[2]), sum(r[3]), sum(r[4])
|
|
FROM
|
|
(
|
|
SELECT
|
|
retention(event_type = 'Browse', event_type = 'Click', event_type = 'Order', event_type = 'Pay') AS r
|
|
FROM funnel_test
|
|
GROUP BY user_id
|
|
);
|
|
sum | sum | sum | sum
|
|
-----+-----+-----+-----
|
|
5 | 5 | 4 | 2
|
|
(1 row)</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p1498211411379">Create the <strong id="EN-US_TOPIC_0000001811515569__b1932641963618">retention_test</strong> table.</p>
|
|
<pre class="screen" id="EN-US_TOPIC_0000001811515569__screen22201165586">CREATE TABLE retention_test(
|
|
uid INT,
|
|
event TEXT,
|
|
event_time TIMESTAMP
|
|
);</pre>
|
|
<p id="EN-US_TOPIC_0000001811515569__p186385404381">Insert data.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen2063804017383"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span>
|
|
<span class="normal">6</span>
|
|
<span class="normal">7</span>
|
|
<span class="normal">8</span>
|
|
<span class="normal">9</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">retention_test</span><span class="w"> </span><span class="k">VALUES</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'pay'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-01'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'login'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-01'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'pay'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-02'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="s1">'login'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-02'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s1">'login'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-01'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="s1">'login'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-02'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="s1">'pay'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-03'</span><span class="p">),</span>
|
|
<span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="s1">'pay'</span><span class="p">,</span><span class="w"> </span><span class="s1">'2024-05-04'</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001811515569__p036619991811">Gather statistics on the retention rate of each user in the payment event after one or two days.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001811515569__screen187371329152212"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
|
|
<span class="normal"> 2</span>
|
|
<span class="normal"> 3</span>
|
|
<span class="normal"> 4</span>
|
|
<span class="normal"> 5</span>
|
|
<span class="normal"> 6</span>
|
|
<span class="normal"> 7</span>
|
|
<span class="normal"> 8</span>
|
|
<span class="normal"> 9</span>
|
|
<span class="normal">10</span>
|
|
<span class="normal">11</span>
|
|
<span class="normal">12</span>
|
|
<span class="normal">13</span>
|
|
<span class="normal">14</span>
|
|
<span class="normal">15</span>
|
|
<span class="normal">16</span>
|
|
<span class="normal">17</span>
|
|
<span class="normal">18</span>
|
|
<span class="normal">19</span>
|
|
<span class="normal">20</span>
|
|
<span class="normal">21</span>
|
|
<span class="normal">22</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">WITH</span><span class="w"> </span><span class="n">retention_count_info</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="p">(</span>
|
|
<span class="w"> </span><span class="k">SELECT</span>
|
|
<span class="w"> </span><span class="n">uid</span><span class="p">,</span>
|
|
<span class="w"> </span><span class="n">range_retention_count</span><span class="p">(</span><span class="n">event</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'login'</span><span class="p">,</span><span class="w"> </span><span class="n">event</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'pay'</span><span class="p">,</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="nb">DATE</span><span class="p">(</span><span class="n">event_time</span><span class="p">),</span><span class="w"> </span><span class="nb">array</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">],</span><span class="w"> </span><span class="s1">'day'</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">info</span>
|
|
<span class="w"> </span><span class="k">FROM</span>
|
|
<span class="w"> </span><span class="n">retention_test</span>
|
|
<span class="w"> </span><span class="k">GROUP</span><span class="w"> </span><span class="k">BY</span>
|
|
<span class="w"> </span><span class="n">uid</span>
|
|
<span class="p">),</span><span class="w"> </span><span class="n">retention_sum</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="p">(</span>
|
|
<span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="n">regexp_split_to_array</span><span class="p">(</span><span class="k">unnest</span><span class="p">(</span><span class="n">range_retention_sum</span><span class="p">(</span><span class="n">info</span><span class="p">)),</span><span class="w"> </span><span class="s1">','</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">s</span>
|
|
<span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">retention_count_info</span>
|
|
<span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="n">to_date</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]::</span><span class="nb">int</span><span class="p">)</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">login_date</span><span class="p">,</span>
|
|
<span class="w"> </span><span class="n">s</span><span class="p">[</span><span class="mi">3</span><span class="p">]::</span><span class="nb">numeric</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">s</span><span class="p">[</span><span class="mi">2</span><span class="p">]::</span><span class="nb">numeric</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">retention_rate1</span><span class="p">,</span>
|
|
<span class="w"> </span><span class="n">s</span><span class="p">[</span><span class="mi">4</span><span class="p">]::</span><span class="nb">numeric</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">s</span><span class="p">[</span><span class="mi">2</span><span class="p">]::</span><span class="nb">numeric</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="n">retention_rate2</span>
|
|
<span class="k">FROM</span><span class="w"> </span><span class="n">retention_sum</span>
|
|
<span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">login_date</span><span class="p">;</span>
|
|
<span class="w"> </span><span class="n">login_date</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">retention_rate1</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">retention_rate2</span>
|
|
<span class="c1">---------------------+-----------------------+------------------------</span>
|
|
<span class="w"> </span><span class="mi">2024</span><span class="o">-</span><span class="mi">05</span><span class="o">-</span><span class="mi">01</span><span class="w"> </span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">.</span><span class="mi">50000000000000000000</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">0</span><span class="p">.</span><span class="mi">00000000000000000000</span>
|
|
<span class="w"> </span><span class="mi">2024</span><span class="o">-</span><span class="mi">05</span><span class="o">-</span><span class="mi">02</span><span class="w"> </span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">.</span><span class="mi">50000000000000000000</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">.</span><span class="mi">50000000000000000000</span>
|
|
<span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="k">rows</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0027.html">Functions and Operators</a></div>
|
|
</div>
|
|
</div>
|
|
|