<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Srilathaa VASU on Medium]]></title>
        <description><![CDATA[Stories by Srilathaa VASU on Medium]]></description>
        <link>https://medium.com/@sri1712lathaa?source=rss-1159fc039de1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sNNFAab4-9-qo3sqD9kFMg.jpeg</url>
            <title>Stories by Srilathaa VASU on Medium</title>
            <link>https://medium.com/@sri1712lathaa?source=rss-1159fc039de1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 15:06:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sri1712lathaa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SQL Cohort Analysis: Are Your Customers Actually Coming Back?]]></title>
            <link>https://medium.com/@sri1712lathaa/sql-cohort-analysis-are-your-customers-actually-coming-back-994615b04f76?source=rss-1159fc039de1------2</link>
            <guid isPermaLink="false">https://medium.com/p/994615b04f76</guid>
            <category><![CDATA[business-intelligence]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[cohort-analysis]]></category>
            <dc:creator><![CDATA[Srilathaa VASU]]></dc:creator>
            <pubDate>Wed, 07 Oct 2026 12:56:22 GMT</pubDate>
            <atom:updated>2026-10-07T12:56:22.973Z</atom:updated>
            <content:encoded><![CDATA[<p>A step-by-step retention analysis, run on 540,000 real transactions</p><p>A company reports 10,000 customers this month, up from 8,000. Good news? Maybe. If 3,000 of those are brand new and 1,000 existing customers quietly left, the headline number is hiding a leak.</p><p>A monthly count can’t separate growth from retention. Cohort analysis can.</p><p>Here’s what it looks like on real data: 541,909 transactions from a UK online retailer, December 2010 to December 2011. Each row is a group of customers who made their first purchase in the same month. Each column is how many of them bought again, n months later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A0Oc7KAmo58xPPJ3ZOuoeA.png" /></figure><p>In this post I’ll build that table in SQL, one query at a time, and then go through what it says. I have used PostgreSQL syntax.</p><h3>What a cohort is ?</h3><p>A cohort is a group of customers who share a starting point, usually the month of their first purchase. Everyone who first bought in January 2011 is the January cohort. Everyone who first bought in February is the February cohort.</p><p>Then you ask one question of each group: how many came back one month later, two months later, three?</p><p>Read the heatmap one row at a time. The Feb 2011 row says that of the customers who first bought in February, 19% bought again in March, 19% in April, and 28% in May. Compare rows and you can see whether customers acquired at different times behave differently.</p><h3><strong>The Data</strong></h3><p>All you need is an orders table with a customer, a date and an amount. I used the UCI Online Retail dataset, which is public.</p><p>One definition before the SQL. Here, a customer counts as retained in month N if they placed at least one order in that month. That’s a choice, not the only definition, but it’s the most common and the easiest to explain.</p><h4>Cleaning it first</h4><p>The raw file would have given wrong cohorts, so it needed work before any cohort logic:</p><ul><li>24.9% of rows have no customer ID. Those rows hold 14.9% of revenue, but you can’t build a cohort without an ID, so they’re out.</li><li>9,288 rows are cancellations, marked by a C at the start of the invoice number. A cancellation isn’t a purchase, so it can’t count as activity.</li><li>2,517 rows have a zero or negative price.</li><li>Three rows labelled “Adjust bad debt” have invoice numbers starting with A. They have no customer ID, so the first filter removes them.</li><li>The file isn’t UTF-8, and the dates are text in month/day/year order.</li></ul><p>That leaves 4,338 customers in 13 monthly cohorts. The cleaning is the first CTE in the Gist at the end of the post.</p><h4>Step 1: Assign each customer to a cohort</h4><p>A customer’s cohort is the month of their earliest order.</p><pre>customer_cohorts AS (<br>    SELECT<br>        customer_id,<br>        DATE_TRUNC(&#39;month&#39;, MIN(order_date)) AS cohort_month<br>    FROM orders<br>    GROUP BY customer_id<br>)</pre><h4>Step 2: One row per customer per active month</h4><pre>customer_activity AS (<br>    SELECT DISTINCT<br>        customer_id,<br>        DATE_TRUNC(&#39;month&#39;, order_date) AS activity_month<br>    FROM orders<br>)</pre><p>The DISTINCT matters. A customer who orders five times in March is still one active customer in March. Skip it and you’re counting orders, not people.</p><h4>Step 3: Count the months since the first purchase</h4><p>Join the two and work out how many months separate each activity month from the cohort month. Month 0 is the first purchase, month 1 is the next calendar month, and so on.</p><pre>cohort_activity AS (<br>    SELECT<br>        a.customer_id,<br>        c.cohort_month,<br>        (EXTRACT(YEAR FROM a.activity_month) - EXTRACT(YEAR FROM c.cohort_month)) * 12<br>        + (EXTRACT(MONTH FROM a.activity_month) - EXTRACT(MONTH FROM c.cohort_month))<br>            AS month_number<br>    FROM customer_activity a<br>    JOIN customer_cohorts c USING (customer_id)<br>)</pre><p>Year difference times twelve, plus month difference. It looks clunky, but it runs on every major database, unlike PostgreSQL’s AGE().</p><h4>Step 4: Turn counts into percentages</h4><pre>cohort_counts AS (<br>    SELECT<br>        cohort_month,<br>        month_number,<br>        COUNT(DISTINCT customer_id) AS active_customers<br>    FROM cohort_activity<br>    GROUP BY cohort_month, month_number<br>),<br><br>retention AS (<br>    SELECT<br>        cohort_month,<br>        month_number,<br>        active_customers,<br>        ROUND(<br>            100.0 * active_customers<br>            / MAX(CASE WHEN month_number = 0 THEN active_customers END)<br>                OVER (PARTITION BY cohort_month),<br>            1<br>        ) AS retention_pct<br>    FROM cohort_counts<br>)</pre><p>Raw counts can’t be compared across cohorts: a cohort of 885 will always beat a cohort of 169. The window function divides each month’s count by that cohort’s month-0 size, which puts every cohort on the same scale.</p><h4>Step 5: Pivot into a matrix</h4><p>The retention table has one row per cohort and month. People read matrices, so pivot it:</p><pre>SELECT<br>    cohort_month,<br>    MAX(CASE WHEN month_number = 0 THEN retention_pct END) AS m0,<br>    MAX(CASE WHEN month_number = 1 THEN retention_pct END) AS m1,<br>    MAX(CASE WHEN month_number = 2 THEN retention_pct END) AS m2,<br>    MAX(CASE WHEN month_number = 3 THEN retention_pct END) AS m3<br>FROM retention<br>GROUP BY cohort_month<br>ORDER BY cohort_month;</pre><p>The full query, all five steps chained together with the cleaning, is here</p><p><a href="https://github.com/Sri1712/Data-Science-Projects-and-Case-Study/tree/fc54fb46c564406b7690a7f38931745926d49432/SQL%20Cohort%20Analysis">Data-Science-Projects-and-Case-Study/SQL Cohort Analysis at fc54fb46c564406b7690a7f38931745926d49432 · Sri1712/Data-Science-Projects-and-Case-Study</a></p><p>Empty cells aren’t errors. Recent cohorts haven’t lived long enough to reach month 3, so the matrix forms a staircase.</p><h4>What the heatmap says</h4><p>Most customers don’t come back the next month. For every 2011 cohort from January to October, month-1 retention sits between 15% and 24%. Averaged across cohorts, 23.7% of customers who bought in a given month bought again the month after.</p><p>Retention doesn’t fall in a straight line. Several cohorts retain better in month 3 than in month 2; February goes from 19% to 28%. That suggests customers who buy in bursts rather than every month, so a miss in month 1 doesn’t mean a lost customer. It’s a guess, and the data alone can’t confirm it. Checking order frequency per customer would.</p><p>Seasonality shows up too. The December 2010 cohort reaches 50% in month 11, which is November 2011, the run-up to Christmas. A product change is an unlikely explanation for that.</p><p>One trap I nearly missed. The data stops on 9 December 2011. If you leave December in, the last diagonal drops sharply: the November cohort shows 11% in month 1, because December is only nine days long. I masked those cells in the heatmap. A partial final month will make your newest cohorts look like they collapsed.</p><h4>Four mistakes worth avoiding</h4><p>Counting orders instead of customers. Ten orders from one person is one retained customer. Use COUNT(DISTINCT customer_id) and the one-row-per-customer-month step.</p><p>Defining the cohort by the wrong date. It should be the first qualifying event, MIN(order_date), not the current month or the latest order.</p><p>Comparing counts instead of percentages. Covered in step 4, but it’s the most common error in retention reports.</p><p>Reading causes into patterns. A stronger cohort doesn’t prove that something you shipped that month worked. Check what else changed.</p><h4>Where to take it next</h4><p>The same pattern works for revenue per cohort, orders per cohort, or retention by acquisition channel. Swap the cohort definition or the metric and the structure holds. Once the SQL works, the output drops straight into Power BI or Tableau as a heatmap.</p><p><em>I write practical guides on SQL, Python, BI and LLM evaluation. Follow along if that’s useful.</em></p><p>#SQL #DataAnalysis #DataScience #BusinessIntelligence #Analytics</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=994615b04f76" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>