runtime
43 / 139

task History, SCD & as-of · hard

Customer segment at order time

Find sale orders placed in November 2011 by identified customers. Return order_id, customer_id and segment: the customer’s segment in the customer_history version valid on the order date.

what to return

Columns
order_idcustomer_idsegment in this order
Rows
333 rows · any order
Skill
Attribute each event to the version valid at its time. Refresher: Latest-state reporting versus SCD Type 2 →

expected result

expected result333 rows · any order
order_idcustomer_idsegment
613917733occasional
614017389key_account
614117166occasional

first 3 of 333 rows

data you can query

orders6 columns · 6,722 rows
order_idnumber · unique idinvoice_notextcustomer_idnumber · links to customersordered_atdate & timecountrytextorder_typetext
1489438181022009-12-01 09:24:00United Kingdomsale
2489517163292009-12-01 11:34:00United Kingdomsale
3C489528167142009-12-01 11:50:00United Kingdomcancellation
4489529179842009-12-01 11:51:00United Kingdomsale
5489532133942009-12-01 11:58:00United Kingdomsale
6489540129132009-12-01 12:18:00United Kingdomsale
7C489541150912009-12-01 12:18:00United Kingdomcancellation
8C489549137682009-12-01 12:33:00United Kingdomcancellation
9C489555169552009-12-01 12:44:00United Kingdomcancellation
10489559170562009-12-01 12:55:00United Kingdomsale
order_idnumber · unique id
1
invoice_notext
489438
customer_idnumber · links to customers
18102
ordered_atdate & time
2009-12-01 09:24:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
2
invoice_notext
489517
customer_idnumber · links to customers
16329
ordered_atdate & time
2009-12-01 11:34:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
3
invoice_notext
C489528
customer_idnumber · links to customers
16714
ordered_atdate & time
2009-12-01 11:50:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
4
invoice_notext
489529
customer_idnumber · links to customers
17984
ordered_atdate & time
2009-12-01 11:51:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
5
invoice_notext
489532
customer_idnumber · links to customers
13394
ordered_atdate & time
2009-12-01 11:58:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
6
invoice_notext
489540
customer_idnumber · links to customers
12913
ordered_atdate & time
2009-12-01 12:18:00
countrytext
United Kingdom
order_typetext
sale
order_idnumber · unique id
7
invoice_notext
C489541
customer_idnumber · links to customers
15091
ordered_atdate & time
2009-12-01 12:18:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
8
invoice_notext
C489549
customer_idnumber · links to customers
13768
ordered_atdate & time
2009-12-01 12:33:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
9
invoice_notext
C489555
customer_idnumber · links to customers
16955
ordered_atdate & time
2009-12-01 12:44:00
countrytext
United Kingdom
order_typetext
cancellation
order_idnumber · unique id
10
invoice_notext
489559
customer_idnumber · links to customers
17056
ordered_atdate & time
2009-12-01 12:55:00
countrytext
United Kingdom
order_typetext
sale

first 10 of 6,722 rows · your queries use all of them

customer_history7 columns · 1,685 rows
history_idnumber · unique idcustomer_idnumber · links to customerssegmenttextcountrytextvalid_fromdatevalid_todateis_currentnumber
112347occasionalIceland2010-10-312010-11-010
212347regularIceland2010-11-01NULL1
312353occasionalBahrain2010-10-27NULL1
412359occasionalCyprus2009-12-052010-01-010
512359regularCyprus2010-01-012011-11-010
612359key_accountCyprus2011-11-01NULL1
712373occasionalAustria2010-02-042010-04-010
812373regularAustria2010-04-012011-04-010
912373occasionalAustria2011-04-01NULL1
1012374occasionalAustria2010-04-302010-05-010
history_idnumber · unique id
1
customer_idnumber · links to customers
12347
segmenttext
occasional
countrytext
Iceland
valid_fromdate
2010-10-31
valid_todate
2010-11-01
is_currentnumber
0
history_idnumber · unique id
2
customer_idnumber · links to customers
12347
segmenttext
regular
countrytext
Iceland
valid_fromdate
2010-11-01
valid_todate
NULL
is_currentnumber
1
history_idnumber · unique id
3
customer_idnumber · links to customers
12353
segmenttext
occasional
countrytext
Bahrain
valid_fromdate
2010-10-27
valid_todate
NULL
is_currentnumber
1
history_idnumber · unique id
4
customer_idnumber · links to customers
12359
segmenttext
occasional
countrytext
Cyprus
valid_fromdate
2009-12-05
valid_todate
2010-01-01
is_currentnumber
0
history_idnumber · unique id
5
customer_idnumber · links to customers
12359
segmenttext
regular
countrytext
Cyprus
valid_fromdate
2010-01-01
valid_todate
2011-11-01
is_currentnumber
0
history_idnumber · unique id
6
customer_idnumber · links to customers
12359
segmenttext
key_account
countrytext
Cyprus
valid_fromdate
2011-11-01
valid_todate
NULL
is_currentnumber
1
history_idnumber · unique id
7
customer_idnumber · links to customers
12373
segmenttext
occasional
countrytext
Austria
valid_fromdate
2010-02-04
valid_todate
2010-04-01
is_currentnumber
0
history_idnumber · unique id
8
customer_idnumber · links to customers
12373
segmenttext
regular
countrytext
Austria
valid_fromdate
2010-04-01
valid_todate
2011-04-01
is_currentnumber
0
history_idnumber · unique id
9
customer_idnumber · links to customers
12373
segmenttext
occasional
countrytext
Austria
valid_fromdate
2011-04-01
valid_todate
NULL
is_currentnumber
1
history_idnumber · unique id
10
customer_idnumber · links to customers
12374
segmenttext
occasional
countrytext
Austria
valid_fromdate
2010-04-30
valid_todate
2010-05-01
is_currentnumber
0

first 10 of 1,685 rows · your queries use all of them

solution.sql
 
Output

Hints and the answer are always available. Using them counts the question as solved with help.

Customer segment at order time — SQL practice · Runtime