Case 6: Customer lifetime value over 12 months

Case 2 showed what a new customer costs. Now for the other side of the ledger: what do they bring in? You work out customer value over a fixed twelve-month window, first as revenue, then as gross margin.

In short: a CTE returns each customer's first completed order. The window ends at date(first_order, '+12 months'), and everything before that counts. Only customers whose window lies completely inside the data are included. For gross margin, calculate from the order lines: quantity * (unit_price - unit_cost).

The situation

CLV stands for customer lifetime value, what a customer is worth over the whole relationship. Nobody knows the whole relationship in advance, so many teams measure a historical value over a fixed window instead, such as the first twelve months. It is a measurement rather than a forecast, and it has one big advantage: customers from different months become directly comparable. Leadership wants to know which acquisition channel brings in the most valuable customers, so the figure can sit next to the cost per new customer.

The data

You need orders for date and status, order_items for quantity and unit_price, products for unit_cost and customers for the channel. The data ends on 31 October 2026, so a twelve-month window is only complete for customers whose first completed order falls between 1 January and 31 October 2025.

SQLite adds months with a modifier in date(). If the day does not exist in the target month, the date rolls over into the next month:

SELECT date('2025-01-24', '+12 months') AS window_end,
       date('2025-10-31', '+1 month') AS rollover;
window_endrollover
2026-01-242025-12-01

For twelve months from a day in 2025 this does not matter: 2026 is not a leap year, and every day exists again a year later.

Task 1: first-year revenue

Task How much revenue does a customer bring in during their first 12 months? Take every customer whose first completed order falls between 1 January and 31 October 2025; only for them is the window complete by the end of October 2026. Show customer_id, first_order (the date) and revenue_12m: the sum of amount over all completed orders from the first one up to, but not including, the same day 12 months later.

orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8

How it works

The CTE finds each customer's MIN(order_date) among completed orders. That is not always the signup_date: customer 7 signed up on 25 February 2025 with an order that was cancelled, and their first completed order came on 30 April. You then join the CTE to orders and only add up orders before the end of the window.

An example: customer 2 bought five times, most recently on 5 March 2026. Their window runs from 24 January 2025 to just before 24 January 2026. Four orders fall inside it, worth 344.4 together. The fifth no longer counts, even though it is part of their total spend of 389.3.

Task 2: customer value and gross margin by channel

Task Which acquisition channel brings in the most valuable customers? Use the same customers and the same 12-month window as in task 1, but calculate from the order items. For each channel (from customers), show the number of customers, the average revenue_12m (sum of quantity * unit_price per customer, average to 2 decimal places) and the average margin_12m, the gross margin (sum of quantity * (unit_price - unit_cost) per customer, average to 2 decimal places).

customers 140 rows, 6 columns
customer_idnamecitysignup_datechannelcampaign_id
1Sophia CampbellLeeds2025-01-16Paid Search1
2Aiden AllenLeeds2025-01-24OrganicNULL
3Aiden MillerAustin2025-01-28OrganicNULL
4Isla WrightGlasgow2025-02-01Affiliate4
5Henry GarciaGlasgow2025-02-07Email3
6Ryan ThomasPortland2025-02-14OrganicNULL
7Priya MurphyBristol2025-02-25OrganicNULL
8Chloe WilsonLondon2025-03-03Paid Social2
9Amelia EvansManchester2025-03-06Paid Social2
10Jack WilsonSeattle2025-03-15Paid Social2
11Grace CampbellAustin2025-03-17OrganicNULL
12Ava LewisAustin2025-03-29Email3
13Ruby MurphyLeeds2025-03-30Affiliate4
14Samuel KhanAtlanta2025-04-05Affiliate4
15Ruby MillerLeeds2025-04-12Email3
16Noah ThomasLondon2025-04-19Email3
17Leo DaviesDenver2025-04-26OrganicNULL
18Leo WrightChicago2025-05-03OrganicNULL
19Mia TaylorGlasgow2025-05-08OrganicNULL
20Liam RobinsonSeattle2025-05-12Email3
21Ella EvansDenver2025-05-17Paid Social2
22Daniel ScottBoston2025-05-18OrganicNULL
23Amelia WrightManchester2025-05-25Paid Search1
24Freya DaviesAtlanta2025-06-01OrganicNULL
25Noah TaylorBoston2025-06-09Email3
26Ava MillerSeattle2025-06-15Paid Social6
27Lucas WilsonAustin2025-06-16Email3
28Liam KhanPortland2025-06-18Paid Social6
29Lucas WrightGlasgow2025-06-25Paid Social6
30Noah MurphyDenver2025-07-06OrganicNULL
31Grace KhanBristol2025-07-08Paid Search5
32Liam DaviesSeattle2025-07-10Paid Search5
33Mason HallSeattle2025-07-14Paid Social6
34Ethan KhanPortland2025-07-24Paid Social6
35Jack ClarkeLeeds2025-07-28Paid Social6
36Ethan CampbellSeattle2025-08-01Paid Social6
37Harper KingBoston2025-08-06OrganicNULL
38Freya ScottManchester2025-08-09OrganicNULL
39Daniel JohnsonManchester2025-08-14Paid Social6
40Ruby WilsonAtlanta2025-08-19Affiliate4
41Zoe KhanAtlanta2025-08-29OrganicNULL
42Oliver YoungAustin2025-08-29Email3
43Leo KingGlasgow2025-09-03OrganicNULL
44Noah AllenBristol2025-09-06Paid Search5
45Oliver LewisAustin2025-09-15OrganicNULL
46James WalkerAustin2025-09-18Paid Search5
47Ava DaviesSeattle2025-09-19OrganicNULL
48Leo AllenLondon2025-09-29OrganicNULL
49James HallSeattle2025-09-29Paid Search5
50Aiden ThomasBoston2025-10-08OrganicNULL
51Oliver AllenAustin2025-10-11Paid Search5
52Daniel ClarkeGlasgow2025-10-14Paid Search5
53Liam HarrisSeattle2025-10-21Email3
54Lucas CampbellManchester2025-10-24OrganicNULL
55Freya BrownLondon2025-10-29Paid Search5
56Freya YoungSeattle2025-11-04OrganicNULL
57Sophia LewisPortland2025-11-05Paid Search5
58Freya EvansBristol2025-11-18OrganicNULL
59Leo PatelLondon2025-11-23Paid Search5
60Ruby ThomasGlasgow2025-11-23Paid Social8
61Amelia TaylorManchester2025-11-25Email3
62Liam MurphyDenver2025-12-01Paid Social8
63Daniel WalkerChicago2025-12-08OrganicNULL
64Zoe KingManchester2025-12-11OrganicNULL
65James ClarkeLeeds2025-12-15Paid Search5
66Samuel KingGlasgow2025-12-19Paid Search5
67Leo MillerDenver2025-12-25OrganicNULL
68Sophia EvansPortland2025-12-26OrganicNULL
69Sophia MurphyAustin2026-01-06Paid Social10
70Olivia KhanAustin2026-01-06Email3
71James ScottLeeds2026-01-16Paid Search9
72Noah SmithChicago2026-01-17Paid Search9
73Liam ScottLeeds2026-01-23Paid Social10
74Priya ScottPortland2026-01-26OrganicNULL
75Jack DaviesAustin2026-02-02Paid Search9
76Priya CampbellGlasgow2026-02-06Affiliate4
77Olivia AllenSeattle2026-02-07OrganicNULL
78Lucas YoungAustin2026-02-13OrganicNULL
79Emma GarciaBristol2026-02-15OrganicNULL
80Grace BrownLondon2026-02-20Affiliate4
81Zoe TaylorSeattle2026-02-25Paid Search9
82Ava BrownManchester2026-03-01OrganicNULL
83Ryan WilsonSeattle2026-03-07OrganicNULL
84Liam WrightAustin2026-03-11Email3
85Olivia TaylorSeattle2026-03-12Affiliate4
86Ethan EvansBoston2026-03-17OrganicNULL
87Ella ScottBoston2026-03-19Paid Search9
88Daniel HallGlasgow2026-04-01Affiliate4
89Chloe ThomasSeattle2026-04-05Paid Search9
90Harper TaylorDenver2026-04-05Paid Search9
91Ethan KingAustin2026-04-11Paid Search9
92Leo CampbellAustin2026-04-14OrganicNULL
93Noah YoungAtlanta2026-04-19OrganicNULL
94Noah JohnsonDenver2026-04-24Paid Search9
95Daniel EvansGlasgow2026-04-28Affiliate4
96Ella WilsonLondon2026-04-30Paid Search9
97Grace AllenBristol2026-05-10Affiliate4
98Jack SmithBristol2026-05-11Paid Search9
99Grace EvansPortland2026-05-13Affiliate4
100Isla ClarkeLondon2026-05-18Paid Search9
101Daniel AllenSeattle2026-05-19Affiliate4
102Zoe BrownLondon2026-05-25OrganicNULL
103Daniel LewisAtlanta2026-06-01OrganicNULL
104Isla MillerSeattle2026-06-04OrganicNULL
105Daniel WrightManchester2026-06-08OrganicNULL
106Zoe WrightGlasgow2026-06-09OrganicNULL
107Chloe WalkerAustin2026-06-16Affiliate4
108Chloe ScottAustin2026-06-22Paid Search9
109Henry EvansGlasgow2026-06-26Affiliate4
110Ryan ScottGlasgow2026-06-26OrganicNULL
111Ruby GarciaBoston2026-07-02OrganicNULL
112Ryan YoungManchester2026-07-04OrganicNULL
113Priya WalkerDenver2026-07-09Affiliate4
114Chloe HallChicago2026-07-14OrganicNULL
115Jack CampbellManchester2026-07-19OrganicNULL
116Harper ScottManchester2026-07-27Paid Search12
117Freya WrightChicago2026-07-28OrganicNULL
118Jack HallGlasgow2026-08-03OrganicNULL
119Chloe MillerManchester2026-08-07Email3
120Oliver GarciaBoston2026-08-09OrganicNULL
121Daniel KingPortland2026-08-15OrganicNULL
122Ruby HarrisChicago2026-08-18OrganicNULL
123Ruby LewisPortland2026-08-25Paid Search12
124Henry HarrisManchester2026-08-27Paid Search12
125Ethan SmithBristol2026-08-31Email3
126Emma LewisBristol2026-09-03Email3
127Zoe EvansBristol2026-09-07OrganicNULL
128Ethan JohnsonBristol2026-09-13OrganicNULL
129Ella LewisDenver2026-09-19Paid Social13
130Olivia RobinsonSeattle2026-09-20OrganicNULL
131Amelia KingBoston2026-09-22Email3
132Oliver TaylorGlasgow2026-09-26Paid Search12
133James AllenSeattle2026-10-04Paid Social13
134Henry KhanAtlanta2026-10-05Paid Social13
135Mia WrightManchester2026-10-10Paid Search12
136Mia JohnsonLondon2026-10-16Affiliate4
137Grace DaviesLeeds2026-10-20Email3
138Liam PatelBristol2026-10-22Paid Social13
139Oliver WrightSeattle2026-10-27OrganicNULL
140Henry AllenSeattle2026-10-29Email3
orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8
order_items 297 rows, 4 columns
order_idproduct_idquantityunit_price
500121119.9
50012219.9
50021189.9
50023124.9
50029124.9
50037149.9
50047149.9
50048119.9
50051189.9
50063124.9
50079124.9
50081189.9
50082114.9
50085134.9
50095134.9
50103124.9
501121119.9
50127149.9
50131189.9
50149124.9
50159124.9
501613179.9
50175134.9
50181189.9
50191189.9
50193124.9
502016199.9
502113179.9
502219129.9
50235134.9
50241189.9
50258119.9
502623139.9
502713179.9
50281189.9
502921119.9
50292219.9
503015127.9
50318119.9
503213179.9
50335134.9
50349124.9
50352214.9
503621119.9
50371829.9
50383124.9
50391189.9
50392214.9
50407149.9
50415134.9
50421189.9
50435134.9
50441189.9
50442114.9
504511129.9
5046171119
50471189.9
50481819.9
504921119.9
50501189.9
50511189.9
50524112.9
50539124.9
50545134.9
505516199.9
50564212.9
50578119.9
505816199.9
50595134.9
50602219.9
50613124.9
506213179.9
506311129.9
5064241129
50657149.9
50658119.9
506514116.9
50661189.9
50662214.9
506716199.9
50681189.9
50682214.9
50695134.9
50698119.9
50707149.9
507114116.9
50721189.9
50735134.9
50738119.9
507416199.9
50755134.9
50761189.9
50763124.9
50774112.9
507810159.9
507915127.9
508021119.9
50811189.9
50824212.9
508310147.92
50841171.92
50843119.92
508511123.92
508519123.92
50852017.92
508611123.92
508723131.92
508810147.92
508911123.92
50907139.92
509111123.92
50926110.32
50931171.92
50933119.92
509423131.92
5095241103.2
509613163.92
50975127.92
50982227.92
50992217.92
51005127.92
51006210.32
51015127.92
51016110.32
510223131.92
51036110.32
510316179.92
510421115.92
51052229.9
51061189.9
51069124.9
510711129.9
51087149.9
510819129.9
51082019.9
51091189.9
51092114.9
51105134.9
511110159.9
511213179.9
511314116.9
511421119.9
51151189.9
51166212.9
51171189.9
511821119.9
511910159.9
51201829.9
5121171119
51229124.9
512313179.9
51242019.9
51251189.9
51252114.9
51267149.9
512721119.9
51284112.9
512911129.9
513010159.9
51311189.9
51328119.9
513321119.9
51332219.9
513423139.9
513512144.9
513613179.9
513713179.9
513714116.9
513723139.9
51387149.9
51395134.9
51396112.9
514019129.9
514021119.9
51418119.9
51428119.9
51434112.9
51432219.9
51441189.9
51442114.9
51455134.9
514613179.9
514615127.9
51479124.9
51485134.9
51495134.9
51504212.9
51512219.9
5152171119
51533124.9
515419129.9
51557149.9
51561189.9
51564112.9
51573124.9
515811129.9
515913179.9
516011129.9
516111129.9
516219129.9
516312144.9
5164241129
51654212.9
516519129.9
51665134.9
51668119.9
51672114.9
51685134.9
5169241129
5170171119
51701839.9
51713124.9
517212144.9
51733124.9
5173241129
517416199.9
5175241129
51767149.9
517721119.9
51781189.9
51784112.9
51791189.9
51792214.9
51801839.9
51814112.9
518216199.9
5183241129
51843124.9
518516199.9
5186171119
518623139.9
51877149.9
51878119.9
518712144.9
518823139.9
518921119.9
51907149.9
5191171119
51911839.9
51925134.9
51935134.9
519413179.9
519514216.9
51968119.9
519715127.9
519816199.9
519919129.9
5200171119
52011189.9
52014112.9
52021189.9
52035134.9
52036112.9
520415127.9
520512144.9
520610159.9
520611129.9
52071189.9
52074112.9
520723139.9
520813179.9
520814216.9
520916199.9
52102214.9
521121119.9
52122214.9
52126112.9
521319129.9
52142214.9
52153124.9
52161189.9
521721119.9
521813179.9
521814116.9
52191189.9
522012144.9
52211189.9
52221189.9
52231189.9
52244212.9
52257149.9
52263124.9
522715127.9
522819129.9
52299124.9
52309124.9
52319124.9
52327149.9
52332114.9
52343124.9
52355134.9
52365134.9
523710159.9
523711129.9
52388119.9
52391189.9
52392114.9
products 24 rows, 5 columns
product_idnamecategorypriceunit_cost
1Running shoesRunning89.948
2Running socks, 3 pairsRunning14.95.2
3Running shirtRunning24.910.5
4Water bottle 750 mlAccessories12.94.1
5Yoga matFitness34.914
6Cork yoga blockFitness12.94.8
7Dumbbells, pairFitness49.922
8Resistance bands, set of 3Fitness19.96
9Foam rollerFitness24.99
10Bike helmetCycling59.927
11Bike light setCycling29.911.5
12Cycling jerseyCycling44.919
13Hiking backpack 30 lOutdoor79.936
14Hiking socksOutdoor16.96.1
15HeadlampOutdoor27.910.8
16Rain jacketOutdoor99.946
17Tennis racketBall sports11958
18Tennis balls, can of 4Ball sports9.93.4
19Training ball, size 5Ball sports29.911
20Ball pumpBall sports9.93
21Swim gogglesSwimming19.96.5
22Swim capSwimming9.92.6
23Sports bagAccessories39.915
24Heart rate watchAccessories12972

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

How it works

Now five tables come together (see joining multiple tables): the CTE, customers for the channel, orders for date and status, order_items for quantity and price, and products for unit cost. After the joins every order line is a row, so the average per customer is SUM(…) / COUNT(DISTINCT f.customer_id). AVG would average over order lines.

Gross margin subtracts the unit cost from every selling price. What is left has to cover advertising, shipping, staff and, in the end, profit. That makes it the right figure to compare with CAC. Black Friday discounts are already in the unit_price of the order lines, so they are handled automatically.

In the result, Email and Paid Search lead on margin with around 74 per customer, while Paid Social comes to 37.19. Next to the cost per new customer from case 2, the Black Friday paid social campaign at 115 per new customer does not look good, while paid search in the second half of 2025 at 35 does. With 4 to 18 customers per channel, treat this as a pointer, not as proof.

Common mistakes

No window

Adding up every order gives January 2025 customers almost two years and October 2025 customers just over one. The exercise says: "Columns and row count match, but not every value."

Less-than-or-equal at the end of the window

"Up to, but not including, the same day 12 months later" means <. With <=, an order placed exactly on the anniversary counts too. The visible data has none, the hidden data does; the exercise then tells you the result is only right with this data.

First order including cancellations

Without a status filter in the CTE, the window starts at a cancelled order. Two customers whose first completed order came after October 2025 slip into the group: 52 rows instead of 50.

AVG over order lines

In task 2, AVG(quantity * unit_price) gives the average per order line, not per customer. The values come out far too small.

Check your result

  • Task 1: 50 customers, 5665.98 in revenue during their first 12 months, 113.32 on average. The highest value is 344.4 (customer 2).
  • Task 2: customers per channel 4, 8, 18, 10 and 10, adding up to 50. Paid Search: revenue_12m 135.79 and margin_12m 73.82.
  • Cross-check: customers times revenue_12m, added up across all channels, comes back to roughly 5665.98.

In MySQL, PostgreSQL and SQL Server

The end of the window is DATE_ADD(first_order, INTERVAL 12 MONTH) in MySQL, DATEADD(month, 12, first_order) in SQL Server and first_order + interval '12 months' in PostgreSQL. They handle missing days differently from SQLite: according to their documentation, MySQL and SQL Server move a day that does not exist in the target month to the last day of that month. SQLite rolls over into the following month by default, as the example above shows.

FAQ

Is this the "real" CLV?

It is a historical customer value over a fixed window. Predictive models also estimate how long customers will stay. For comparing channels and campaigns, the historical value is often enough, and everyone on the team can follow how it was calculated.

Why gross margin rather than revenue?

A customer who spends 100 is worth 50 at a 50% margin and 20 at a 20% margin. Only the margin tells you how much you can afford to spend on winning that customer.

How do I compare customer value and CAC properly?

On the same basis: customers from the same period, costs from the same period, and gross margin instead of revenue. If a channel's margin per customer is above its CAC, the store makes money on those customers within the window.

Done: all six cases

Once every case task is solved, at least 80% of them without revealing the answer, you get the certificate for SQL for marketing and e-commerce. Your progress is in the course view.

Back to the course and certificate

Write to us

Choose language

Select your preferred language