All articles →

100 Exercises / SQL / 100 Exercises to SQL for Data Analysis

Analyze orders, customers, and inventory with SQL

Calculate order KPIs, repeat rates, RFM scores, and inventory coverage in SQL. Check denominators, reference dates, stock rules, and monthly changes.

SQL 100 Exercises Chapter 8 (No.071–No.080): Practical Data Analysis of SQL

Note

This material is a notebook previously used by Sukari Kobo (or personally by the representative, Hiroshi Wayama), and has been restructured, edited, and published with the company’s permission. All data listed is fictional and has no relation whatsoever to real companies, factories, or figures.

Introduction: Practical Challenges in Manufacturing Covered in This Article

In manufacturing, sales, sales, and inventory management, the following questions arise every month.

  • “How many orders were received this month, and for what value? How much did that change from last month?”
  • “How do value per customer and average value per order differ?”
  • “Which customers are you placing the most orders with?”
  • “How many new clients did you do business with for the first time this year?”
  • “What percentage of customers have placed orders more than twice (repeat purchase rate)?”
  • “Which customer did you place the last order with a long time ago? (Dormant customer)”
  • “I want to distinguish VIP customers from dormant customers through RFM analysis.”
  • “I want to compare inventory with shipment performance over the past three months to check for shortages or surpluses.”
  • “Want to detect products at risk of out-of-stock early”
  • “I want to automatically detect products with abnormal fluctuations in orders.”

In this chapter, we will put these Most frequently used in practice SQL pattern into practice with manufacturing order and inventory data.

TableOverviewnumber of cases
customersClient Master (Manufacturing B2B Clients)20 companies
productsProduct Master10 Items
ordersOrder History (January–December 2024)About 620 items
inventoryInventory Table (as of the end of December 2024)10 Items

Common workplace tasks

Imagine the tasks a sales manager in a mid-sized manufacturing company does at the end of each month.

  1. Open multiple Excels and manually create copy & paste → Monthly Sales Report
  2. Visually check whether this client placed an order last month as well→ Identifying repeat customers
  3. Checking inventory shortage risks relies on the instincts of the person in charge→ Inventory alerts
  4. Sudden sales spikes or decreases are often noticed afterward→ anomaly detection

Save monthly aggregation and classification rules in SQL so that scheduled jobs and BI tools can repeat them. Measure the actual time saved, including data retrieval and result review.

Common pitfalls

Order totals depend on whether you group by month, customer, or product. Inventory analysis also requires a defined demand period and replenishment lead time.

comparative axisdifficultyCorresponding exercise
Time Series (Monthly Transition)Influence of seasonality and external factorsNo.071, 080
By customer (who is buying what)Large customer base, making manual aggregation difficultNo.072, 073
Customer status changes (new/continuous/dormant)Combining time axis and customer axisNo.074, 075, 076
Customer Overall Rating (RFM)Simultaneous Implementation of 3-Dimensional EvaluationNo.077
Inventory and demand matchingConsideration of production planning and order lead timesNo.078, 079

Putting RFM calculations and classification rules in SQL makes them reproducible for periodic updates and BI reporting.

Exercises in this chapter

No.ThemeSQL FeaturesApplications in Manufacturing
071Aggregate sales KPIsGROUP BY + Aggregate FunctionAutomatic Generation of Monthly Order Summaries
072Calculate the average order valueAVG, SUM / COUNTCalculation of Average Order Amount by Customer
073Aggregate purchase frequency by customerGROUP BY + ORDER BYIdentifying VIP Customers and Heavy Users
074Classify new and existing customersCASE + DATE ComparisonDeveloping new customers vs. deepening existing ones
075Calculating the repeat rateAggregation + Conditional CalculationSetting Order Retention Rate as a KPI
076Calculate the last purchase dateMAX(date) + date differenceExtracting dormant customers and taking sales actions
077Creating data for RFM analysisCTE + CASE WHENAutomatic customer segment classification
078Compare inventory and sales numbersLEFT JOIN + AggregateQuantitative Tracking of Inventory Overage and Shortage
079Extracting products at risk of out-of-stockCTE + CASE WHENAutomatic generation of replenishment order alerts
080Extract products with abnormally high or low salesCTE + LAG Window FunctionAutomatic detection of order anomalies

Preparing the Python environment

Run each article in a fresh Jupyter Notebook kernel, from top to bottom, including the folded setup cells. [IN: n] and [OUT: n] identify matching input and output. The outputs were checked by rerunning the examples; version information and query plans may differ by environment. The font setting targets macOS; choose an installed Japanese font on other systems. Existing shared charts retain their original Japanese labels.

[IN: 1]
import platform

print(f"OS: {platform.system()} {platform.release()}")
print(f"Python: {platform.python_version()}")
[OUT: 1]
OS: Darwin 25.5.0
Python: 3.14.4
Show setup code and output (input 2)
[IN: 2]
import sqlite3
from datetime import date, timedelta
import numpy as np
import polars as pl
import matplotlib
import matplotlib.pyplot as plt

matplotlib.rcParams['font.family'] = 'Hiragino Maru Gothic Pro'
%config InlineBackend.figure_format = 'svg'
np.random.seed(42)


def q(conn, sql):
    '''SQL Run Polars DataFrame Display results'''
    print('── SQL ─────────────────────────────────────────')
    for line in sql.strip().split('\n'):
        print(f'  {line}')
    print('───────────────────────────────────────────────')
    cur = conn.execute(sql.strip())
    rows = cur.fetchall()
    cols = [d[0] for d in cur.description]
    data = {col: [row[i] for row in rows] for i, col in enumerate(cols)}
    df = pl.DataFrame(data)
    print(df)
    print(f'↳ {len(rows)} rows retrieved')
    return df


print(f"sqlite3   : {sqlite3.sqlite_version}")
print(f"polars    : {pl.__version__}")
print(f"numpy     : {np.__version__}")
print(f"matplotlib: {matplotlib.__version__}")
print()
print("Library loading complete")
[OUT: 2]
sqlite3   : 3.50.4
polars    : 1.42.1
numpy     : 2.5.1
matplotlib: 3.11.0

Library loading complete

Creating sample data

Show setup code and output (input 3)
[IN: 3]
conn = sqlite3.connect(':memory:')

# ── customers ─────────────────────────────────────────────
conn.execute('''
CREATE TABLE customers (
    customer_id         TEXT PRIMARY KEY,
    customer_name       TEXT,
    region              TEXT,
    first_contract_date TEXT
)
''')
CUST_DATA = [
    ('C001', 'Yamada Machinery Works',       'Kanto', '2022-03-15'),
    ('C002', 'Tanaka Steel Co., Ltd.',     'Kansai', '2021-07-20'),
    ('C003', 'Suzuki Precision Industry',         'East Sea', '2022-11-05'),
    ('C004', 'Takahashi Engineering', 'Kanto', '2023-04-18'),
    ('C005', 'Ito Kogyo Co., Ltd.',     'Kansai', '2021-09-30'),
    ('C006', 'Watanabe Machinery Industry',         'Kyushu', '2023-01-10'),
    ('C007', 'Nakamura Manufacturing',           'East Sea', '2022-06-22'),
    ('C008', 'Kobayashi Seiki',             'Kanto', '2024-02-15'),
    ('C009', 'Kato Heavy Industries',           'Kansai', '2021-12-01'),
    ('C010', 'Yoshida Mechatronics',   'Kanto', '2024-04-08'),
    ('C011', 'Yamamoto Industry',             'East Sea', '2022-08-14'),
    ('C012', 'Matsumoto Manufacturing',           'Kyushu', '2023-02-28'),
    ('C013', 'Sato Machinery and Electronics Industry',         'Kanto', '2024-07-03'),
    ('C014', 'Shimizu Seiko',             'Kansai', '2021-05-16'),
    ('C015', 'Ichikawa Kogyo Co., Ltd.',     'East Sea', '2022-10-19'),
    ('C016', 'Kimura Manufacturing',           'Kyushu', '2023-06-30'),
    ('C017', 'Hashimoto Machinery',             'Kanto', '2024-01-22'),
    ('C018', 'Ishikawa Industry',             'Kansai', '2021-11-08'),
    ('C019', 'Arakawa Seiki',             'East Sea', '2022-04-25'),
    ('C020', 'Fujita Manufacturing',           'Kyushu', '2023-09-12'),
]
conn.executemany('INSERT INTO customers VALUES (?,?,?,?)', CUST_DATA)

# ── products ──────────────────────────────────────────────
conn.execute('''
CREATE TABLE products (
    product_id     TEXT PRIMARY KEY,
    product_name   TEXT,
    category       TEXT,
    standard_price INTEGER,
    lead_time_days INTEGER
)
''')
PROD_DATA = [
    ('P001', 'Precision Bearings',     'Machine parts', 2800,  7),
    ('P002', 'hydraulic cylinder',     'Machine parts', 15000, 10),
    ('P003', 'motor coil',     'electrical components', 4500,   5),
    ('P004', 'Control board',           'electrical components', 28000, 14),
    ('P005', 'Industrial Rubber Packing', 'consumables',    350,   3),
    ('P006', 'Stainless steel flange', 'Machine parts',  6800,  7),
    ('P007', 'three-phase motor',       'electrical components', 35000, 14),
    ('P008', 'heat-resistant gasket',     'consumables',    850,   3),
    ('P009', 'rolling bearing',         'Machine parts',  3200,  5),
    ('P010', 'electromagnetic valve',         'electrical components', 12000, 10),
]
conn.executemany('INSERT INTO products VALUES (?,?,?,?,?)', PROD_DATA)
PROD_DICT = {p[0]: p[3] for p in PROD_DATA}

# ── orders ────────────────────────────────────────────────
conn.execute('''
CREATE TABLE orders (
    order_id   INTEGER PRIMARY KEY,
    order_date TEXT,
    customer_id TEXT,
    product_id  TEXT,
    quantity    INTEGER,
    unit_price  INTEGER
)
''')

# Standard Order Quantity for Each Product
BASE_QTY = {'P001':80,'P002':8,'P003':40,'P004':3,'P005':400,
            'P006':15,'P007':2,'P008':150,'P009':60,'P010':8}

# Start Month of New Customers (within 2024)
NEW_START = {'C008':2, 'C010':4, 'C013':7, 'C017':1}

CONTRACT_DATES = {c[0]: date.fromisoformat(c[3]) for c in CUST_DATA}

CUST_IDS = [f'C{i:03d}' for i in range(1, 21)]
PROD_IDS = [f'P{i:03d}' for i in range(1, 11)]

CUST_W = np.array([3.0,2.5,2.0,1.5,2.0,1.0,1.8,0.8,2.2,1.2,
                   1.5,0.7,0.5,1.8,1.2,0.8,1.0,2.0,1.4,0.9])
PROD_W = np.array([3.0,2.0,2.5,1.0,4.0,2.0,0.8,3.5,2.5,1.5])
MONTH_FAC = {1:0.85,2:0.80,3:1.10,4:0.95,5:0.90,6:1.05,
             7:0.85,8:0.75,9:1.00,10:1.05,11:1.10,12:1.20}

orders_rows = []
oid = 1

# Basic orders from all major clients (number of orders proportional to each company's weight)
for ci, cid in enumerate(CUST_IDS):
    sm = NEW_START.get(cid, 1)
    n = max(int(CUST_W[ci] * 8), 3)
    for _ in range(n):
        month = np.random.randint(sm, 13)
        day   = np.random.randint(1, 28)
        # Do not generate an order before the contract date.
        od    = max(date(2024, month, day), CONTRACT_DATES[cid])

        pi    = np.random.choice(10, p=PROD_W / PROD_W.sum())
        pid   = PROD_IDS[pi]
        qty   = max(int(BASE_QTY[pid] * np.random.uniform(0.5, 2.0)), 1)
        up    = int(PROD_DICT[pid] * np.random.uniform(0.92, 1.08))
        orders_rows.append((oid, str(od), cid, pid, qty, up))
        oid += 1

# Additional random orders (with monthly seasonal fluctuations)
for _ in range(400):
    ci  = np.random.choice(20, p=CUST_W / CUST_W.sum())
    cid = CUST_IDS[ci]
    sm  = NEW_START.get(cid, 1)
    month = np.random.randint(sm, 13)
    fac   = MONTH_FAC[month]
    if np.random.rand() > fac:
        continue  # High order probability during busy periods
    day = np.random.randint(1, 28)
    od  = max(date(2024, month, day), CONTRACT_DATES[cid])

    pi  = np.random.choice(10, p=PROD_W / PROD_W.sum())
    pid = PROD_IDS[pi]
    qty = max(int(BASE_QTY[pid] * np.random.uniform(0.5, 2.0)), 1)
    up  = int(PROD_DICT[pid] * np.random.uniform(0.92, 1.08))
    orders_rows.append((oid, str(od), cid, pid, qty, up))
    oid += 1

conn.executemany('INSERT INTO orders VALUES (?,?,?,?,?,?)', orders_rows)

# ── inventory ──────────────────────────────────────────────
conn.execute('''
CREATE TABLE inventory (
    product_id    TEXT PRIMARY KEY,
    stock_qty     INTEGER,
    safety_stock  INTEGER,
    last_updated  TEXT
)
''')
INV_DATA = [
    ('P001', 850,  300, '2024-12-31'),
    ('P002', 120,  150, '2024-12-31'),
    ('P003', 380,  200, '2024-12-31'),
    ('P004',  25,   40, '2024-12-31'),
    ('P005',5000, 1500, '2024-12-31'),
    ('P006', 280,  150, '2024-12-31'),
    ('P007',   8,   15, '2024-12-31'),
    ('P008',3200, 1000, '2024-12-31'),
    ('P009', 450,  300, '2024-12-31'),
    ('P010',  65,   80, '2024-12-31'),
]
conn.executemany('INSERT INTO inventory VALUES (?,?,?,?)', INV_DATA)
conn.commit()

print(f"customers: {len(CUST_DATA)} records")
print(f"products : {len(PROD_DATA)} records")
print(f"orders   : {len(orders_rows)} records")
print(f"inventory: {len(INV_DATA)} records")
[OUT: 3]
customers: 20 records
products : 10 records
orders   : 607 records
inventory: 10 records
[IN: 4]
for tbl in ['customers','products','orders','inventory']:
    n = conn.execute(f'SELECT COUNT(*) FROM {tbl}').fetchone()[0]
    print(f'{tbl:12s}: {n:5d} records')
print()
q(conn, 'SELECT * FROM products')
[OUT: 4]
customers   :    20 records
products    :    10 records
orders      :   607 records
inventory   :    10 records

── SQL ─────────────────────────────────────────
  SELECT * FROM products
───────────────────────────────────────────────
shape: (10, 5)
┌────────────┬───────────────────────────┬───────────────────────┬────────────────┬────────────────┐
│ product_id ┆ product_name              ┆ category              ┆ standard_price ┆ lead_time_days │
│ ---        ┆ ---                       ┆ ---                   ┆ ---            ┆ ---            │
│ str        ┆ str                       ┆ str                   ┆ i64            ┆ i64            │
╞════════════╪═══════════════════════════╪═══════════════════════╪════════════════╪════════════════╡
│ P001       ┆ Precision Bearings        ┆ Machine parts         ┆ 2800           ┆ 7              │
│ P002       ┆ hydraulic cylinder        ┆ Machine parts         ┆ 15000          ┆ 10             │
│ P003       ┆ motor coil                ┆ electrical components ┆ 4500           ┆ 5              │
│ P004       ┆ Control board             ┆ electrical components ┆ 28000          ┆ 14             │
│ P005       ┆ Industrial Rubber Packing ┆ consumables           ┆ 350            ┆ 3              │
│ P006       ┆ Stainless steel flange    ┆ Machine parts         ┆ 6800           ┆ 7              │
│ P007       ┆ three-phase motor         ┆ electrical components ┆ 35000          ┆ 14             │
│ P008       ┆ heat-resistant gasket     ┆ consumables           ┆ 850            ┆ 3              │
│ P009       ┆ rolling bearing           ┆ Machine parts         ┆ 3200           ┆ 5              │
│ P010       ┆ electromagnetic valve     ┆ electrical components ┆ 12000          ┆ 10             │
└────────────┴───────────────────────────┴───────────────────────┴────────────────┴────────────────┘
↳ 10 rows retrieved

No.071: Aggregating Sales KPIs

Practical use

We summarize monthly order count, value, and quantity. Order value is used as a proxy for revenue here; it is distinct from recognized revenue and shipment records. This dataset contains no prior-year records, so it does not support year-over-year calculations.

How the SQL works

For monthly aggregation, extract the year and month by strftime('%Y-%m', order_date) and aggregate by GROUP BY.

monthly sales=∑iquantityi×unit_pricei\text{monthly sales} = \sum_{i} \text{quantity}_i \times \text{unit\_price}_i

Definition of Key KPIs:

KPISQL RepresentationMeaning
Number of orders receivedCOUNT(order_id)How many orders were placed each month
Sales amountSUM(quantity * unit_price)Total Order Amount for Month
Average Price per CustomerSUM(amount) / COUNT(DISTINCT customer_id)Average monthly amount per company
Average Order AmountSUM(amount) / COUNT(order_id)Average amount per order

Run in Python

[IN: 5]
print("=== No.071 salesKPIAggregate ===\n")

# (1) Monthly Order KPI
print("① Monthly orders KPI summary")
df71 = q(conn, '''
SELECT
  strftime('%Y-%m', order_date)           AS ym,
  COUNT(order_id)                         AS order_count,
  COUNT(DISTINCT customer_id)             AS unique_customers,
  SUM(quantity * unit_price)              AS total_revenue,
  ROUND(SUM(quantity * unit_price) * 1.0
        / COUNT(order_id), 0)             AS avg_order_value
FROM orders
GROUP BY ym
ORDER BY ym
''')

# (2) Visualization: Monthly sales + number of orders (two-axis graph)
YMS     = df71['ym'].to_list()
REV     = [v / 1_000_000 for v in df71['total_revenue'].to_list()]  # million yen
ORDERS  = df71['order_count'].to_list()

fig, ax1 = plt.subplots(figsize=(10, 5))
ax2 = ax1.twinx()
ax1.bar(YMS, REV,    color='#3498db', alpha=0.7, label='Sales amount (million yen)')
ax2.plot(YMS, ORDERS, color='#e74c3c', marker='o', linewidth=2, label='Number of orders received')
ax1.set_title('monthly Order received KPI(No.071: Sales amount + Number of Orders)', fontsize=13)
ax1.set_xlabel('Year and month')
ax1.set_ylabel('Sales amount (million yen)')
ax2.set_ylabel('Number of orders received')
ax1.grid(axis='y', alpha=0.3)
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left', fontsize=9)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
[OUT: 5]
=== No.071 salesKPIAggregate ===

① Monthly orders KPI summary
── SQL ─────────────────────────────────────────
  SELECT
    strftime('%Y-%m', order_date)           AS ym,
    COUNT(order_id)                         AS order_count,
    COUNT(DISTINCT customer_id)             AS unique_customers,
    SUM(quantity * unit_price)              AS total_revenue,
    ROUND(SUM(quantity * unit_price) * 1.0
          / COUNT(order_id), 0)             AS avg_order_value
  FROM orders
  GROUP BY ym
  ORDER BY ym
───────────────────────────────────────────────
shape: (12, 5)
┌─────────┬─────────────┬──────────────────┬───────────────┬─────────────────┐
│ ym      ┆ order_count ┆ unique_customers ┆ total_revenue ┆ avg_order_value │
│ ---     ┆ ---         ┆ ---              ┆ ---           ┆ ---             │
│ str     ┆ i64         ┆ i64              ┆ i64           ┆ f64             │
╞═════════╪═════════════╪══════════════════╪═══════════════╪═════════════════╡
│ 2024-01 ┆ 44          ┆ 16               ┆ 8529495       ┆ 193852.0        │
│ 2024-02 ┆ 39          ┆ 15               ┆ 7383975       ┆ 189333.0        │
│ 2024-03 ┆ 54          ┆ 16               ┆ 10700710      ┆ 198161.0        │
│ 2024-04 ┆ 56          ┆ 18               ┆ 10019652      ┆ 178922.0        │
│ 2024-05 ┆ 45          ┆ 18               ┆ 7893838       ┆ 175419.0        │
│ …       ┆ …           ┆ …                ┆ …             ┆ …               │
│ 2024-08 ┆ 46          ┆ 19               ┆ 7141359       ┆ 155247.0        │
│ 2024-09 ┆ 60          ┆ 20               ┆ 10054828      ┆ 167580.0        │
│ 2024-10 ┆ 57          ┆ 17               ┆ 10143009      ┆ 177948.0        │
│ 2024-11 ┆ 48          ┆ 19               ┆ 8951475       ┆ 186489.0        │
│ 2024-12 ┆ 65          ┆ 18               ┆ 10576193      ┆ 162711.0        │
└─────────┴─────────────┴──────────────────┴───────────────┴─────────────────┘
↳ 12 rows retrieved

Chart 1 for input 5

Interpreting the results

  • Compare order values across months. The generator varies order frequency by month; the result is not evidence of real seasonal or fiscal-year demand effects.
  • If the avg_order_value (average order amount) fluctuates significantly from month to month, check whether large orders are being accepted
  • Months with few unique_customers (number of unique customers) may be highly dependent on specific large clients
    Can be used to understand → Sales concentration risk

No.072: Calculating Average Customer Value

Practical use

This exercise calculates average value per order for each customer. It differs from the monthly value per customer in No.071 because the denominator is the number of orders.

Average order value=Total order valueOrder count.\text{Average order value}=\frac{\text{Total order value}}{\text{Order count}}.

By reviewing both the annual total order amount and the number of orders, you can understand the Trading Characteristics of your clients.

FeaturesHigh unit price, low frequencyLow Unit Price and High Frequency
ExampleHigh-Value Equipment & Made-to-Order ProductsConsumables & Regular Order Items
Sales SupportAssign a dedicated person for careful follow-upPromoting Automated Ordering and EDI Integration
RisksIf transactions stop, it will have a significant impact on sales.Prone to price wars

Run in Python

[IN: 6]
print("=== No.072 Calculate the average order value ===\n")

# (1) Annual Average Customer Value by Client (Top 10 Companies)
print("① Departing in triumph Annual Average Passenger Price (descending order)")
df72 = q(conn, '''
SELECT
  o.customer_id,
  c.customer_name,
  c.region,
  COUNT(o.order_id)                              AS order_count,
  SUM(o.quantity * o.unit_price)                 AS total_revenue,
  ROUND(SUM(o.quantity * o.unit_price) * 1.0
        / COUNT(o.order_id), 0)                  AS avg_order_value
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY o.customer_id
ORDER BY avg_order_value DESC
''')

# (2) Overall average customer value
print("\n② Overall Average Spend per Passenger")
q(conn, '''
SELECT ROUND(SUM(quantity * unit_price) * 1.0 / COUNT(order_id), 0) AS overall_avg
FROM orders
''')
[OUT: 6]
=== No.072 Calculate the average order value ===

① Departing in triumph Annual Average Passenger Price (descending order)
── SQL ─────────────────────────────────────────
  SELECT
    o.customer_id,
    c.customer_name,
    c.region,
    COUNT(o.order_id)                              AS order_count,
    SUM(o.quantity * o.unit_price)                 AS total_revenue,
    ROUND(SUM(o.quantity * o.unit_price) * 1.0
          / COUNT(o.order_id), 0)                  AS avg_order_value
  FROM orders o
  JOIN customers c ON o.customer_id = c.customer_id
  GROUP BY o.customer_id
  ORDER BY avg_order_value DESC
───────────────────────────────────────────────
shape: (20, 6)
┌─────────────┬─────────────────────────┬──────────┬─────────────┬───────────────┬─────────────────┐
│ customer_id ┆ customer_name           ┆ region   ┆ order_count ┆ total_revenue ┆ avg_order_value │
│ ---         ┆ ---                     ┆ ---      ┆ ---         ┆ ---           ┆ ---             │
│ str         ┆ str                     ┆ str      ┆ i64         ┆ i64           ┆ f64             │
╞═════════════╪═════════════════════════╪══════════╪═════════════╪═══════════════╪═════════════════╡
│ C012        ┆ Matsumoto Manufacturing ┆ Kyushu   ┆ 10          ┆ 2080769       ┆ 208077.0        │
│ C014        ┆ Shimizu Seiko           ┆ Kansai   ┆ 44          ┆ 8495483       ┆ 193079.0        │
│ C007        ┆ Nakamura Manufacturing  ┆ East Sea ┆ 47          ┆ 9068969       ┆ 192957.0        │
│ C005        ┆ Ito Kogyo Co., Ltd.     ┆ Kansai   ┆ 48          ┆ 9205481       ┆ 191781.0        │
│ C010        ┆ Yoshida Mechatronics    ┆ Kanto    ┆ 22          ┆ 4206247       ┆ 191193.0        │
│ …           ┆ …                       ┆ …        ┆ …           ┆ …             ┆ …               │
│ C020        ┆ Fujita Manufacturing    ┆ Kyushu   ┆ 21          ┆ 3398339       ┆ 161826.0        │
│ C002        ┆ Tanaka Steel Co., Ltd.  ┆ Kansai   ┆ 38          ┆ 6126261       ┆ 161217.0        │
│ C017        ┆ Hashimoto Machinery     ┆ Kanto    ┆ 23          ┆ 3634446       ┆ 158019.0        │
│ C013        ┆ Sato Machinery and      ┆ Kanto    ┆ 8           ┆ 1259088       ┆ 157386.0        │
│             ┆ Electronics…            ┆          ┆             ┆               ┆                 │
│ C008        ┆ Kobayashi Seiki         ┆ Kanto    ┆ 13          ┆ 1922514       ┆ 147886.0        │
└─────────────┴─────────────────────────┴──────────┴─────────────┴───────────────┴─────────────────┘
↳ 20 rows retrieved

② Overall Average Spend per Passenger
── SQL ─────────────────────────────────────────
  SELECT ROUND(SUM(quantity * unit_price) * 1.0 / COUNT(order_id), 0) AS overall_avg
  FROM orders
───────────────────────────────────────────────
shape: (1, 1)
┌─────────────┐
│ overall_avg │
│ ---         │
│ f64         │
╞═════════════╡
│ 178204.0    │
└─────────────┘
↳ 1 rows retrieved

Interpreting the results

  • Customers with high average customer value tend to place more High-end products (control boards, three-phase motors) orders
  • Even if the average transaction value is low, customers with a high number of orders become a stable sales base as Regular ordering location for consumables.
  • If the top per-customer value rankings and the top order volume rankings do not match, it serves as a starting point for analyzing the low profits but high volume vs High Added Value’s transaction structure

No.073: Aggregating Purchase Frequency by Customer

Practical use

By understanding “how many times a year you place orders” by client,
Identifying Loyal Customers, Engaging with Low-Frequency Customers becomes possible.

How the SQL works

Frequency is the “F” component in RFM analysis.

Fi=COUNT(order_id) WHERE customer_id=iF_i = \text{COUNT}(\text{order\_id}) \text{ WHERE customer\_id} = i

Inspect order frequency and value separately. Do not assume the Pareto rule—20% of customers account for 80% of revenue—holds in this dataset.

Run in Python

[IN: 7]
print("=== No.073 Aggregate purchase frequency by customer ===\n")

# (1) Number of orders by client (total)
print("① Departing in triumph Number of orders received")
df73 = q(conn, '''
SELECT
  o.customer_id,
  c.customer_name,
  c.region,
  COUNT(o.order_id)              AS order_count,
  SUM(o.quantity * o.unit_price) AS total_revenue
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY o.customer_id
ORDER BY order_count DESC
''')

# (2) Visualization: Top 10 Order Counts (Bar Graph)
df73_top = df73.head(10)
names  = df73_top['customer_name'].to_list()
counts = df73_top['order_count'].to_list()
revs   = [v / 1_000_000 for v in df73_top['total_revenue'].to_list()]

fig, axes = plt.subplots(1, 2, figsize=(13, 5))

# Number of orders received
axes[0].barh(names[::-1], counts[::-1], color='#3498db', edgecolor='white')
axes[0].set_title('Departing in triumph Number of orders received TOP10', fontsize=12)
axes[0].set_xlabel('Number of orders received')
axes[0].set_ylabel('')
axes[0].grid(axis='x', alpha=0.4)

# Sales amount
axes[1].barh(names[::-1], revs[::-1], color='#2ecc71', edgecolor='white')
axes[1].set_title('Departing in triumph Sales amount TOP10(Million yen)', fontsize=12)
axes[1].set_xlabel('Sales amount (million yen)')
axes[1].set_ylabel('')
axes[1].grid(axis='x', alpha=0.4)

plt.tight_layout()
plt.show()
[OUT: 7]
=== No.073 Aggregate purchase frequency by customer ===

① Departing in triumph Number of orders received
── SQL ─────────────────────────────────────────
  SELECT
    o.customer_id,
    c.customer_name,
    c.region,
    COUNT(o.order_id)              AS order_count,
    SUM(o.quantity * o.unit_price) AS total_revenue
  FROM orders o
  JOIN customers c ON o.customer_id = c.customer_id
  GROUP BY o.customer_id
  ORDER BY order_count DESC
───────────────────────────────────────────────
shape: (20, 5)
┌─────────────┬─────────────────────────────────┬──────────┬─────────────┬───────────────┐
│ customer_id ┆ customer_name                   ┆ region   ┆ order_count ┆ total_revenue │
│ ---         ┆ ---                             ┆ ---      ┆ ---         ┆ ---           │
│ str         ┆ str                             ┆ str      ┆ i64         ┆ i64           │
╞═════════════╪═════════════════════════════════╪══════════╪═════════════╪═══════════════╡
│ C001        ┆ Yamada Machinery Works          ┆ Kanto    ┆ 58          ┆ 10020852      │
│ C005        ┆ Ito Kogyo Co., Ltd.             ┆ Kansai   ┆ 48          ┆ 9205481       │
│ C007        ┆ Nakamura Manufacturing          ┆ East Sea ┆ 47          ┆ 9068969       │
│ C009        ┆ Kato Heavy Industries           ┆ Kansai   ┆ 45          ┆ 7609834       │
│ C014        ┆ Shimizu Seiko                   ┆ Kansai   ┆ 44          ┆ 8495483       │
│ …           ┆ …                               ┆ …        ┆ …           ┆ …             │
│ C020        ┆ Fujita Manufacturing            ┆ Kyushu   ┆ 21          ┆ 3398339       │
│ C016        ┆ Kimura Manufacturing            ┆ Kyushu   ┆ 16          ┆ 2648905       │
│ C008        ┆ Kobayashi Seiki                 ┆ Kanto    ┆ 13          ┆ 1922514       │
│ C012        ┆ Matsumoto Manufacturing         ┆ Kyushu   ┆ 10          ┆ 2080769       │
│ C013        ┆ Sato Machinery and Electronics… ┆ Kanto    ┆ 8           ┆ 1259088       │
└─────────────┴─────────────────────────────────┴──────────┴─────────────┴───────────────┘
↳ 20 rows retrieved

Chart 1 for input 7

Interpreting the results

  • There is a possibility that the top order number and top sales amount do not match Order frequency is low, but large orders are made. clients.
  • Clients with extremely low order volumes (1–2) may be dormant candidate uncertain if the next order will come
  • Verification of Pareto’s Law: Check whether the top few companies account for 80% of total sales and use it to review Sales Resource Allocation

No.074: Classifying New and Existing Customers

Practical use

By classifying “clients who have started trading with this year” and “clients who have been continuously trading for a long time,“
You can simultaneously grasp Effects of New Acquisition (results of business investment) and Deepening Existing Customers.

How the SQL works

customers.first_contract_date (initial contract date) is used as the basis for classification using the CASE formula.

customertype={New customerif first_contract_date≥’2024-01-01’Existing Customerotherwise\text{customertype} = \begin{cases} \text{New customer} & \text{if } \text{first\_contract\_date} \geq \text{'2024-01-01'} \\ \text{Existing Customer} & \text{otherwise} \end{cases}
indicatorMeaning
Number of new customersNumber of new developments this year
Total Order Amount from New CustomersContribution of New Acquisition Sales
Average customer value for new customersTransaction Size in the First Year

Run in Python

[IN: 8]
print("=== No.074 Classify new and existing customers ===\n")

# (1) Summary of New vs. Existing Classification
print("① new / existing Client Summary (2024 (Based on year)")
df74a = q(conn, '''
SELECT
  CASE
    WHEN c.first_contract_date >= '2024-01-01' THEN 'New customers (2024New contracts every year)'
    ELSE 'Existing Customers (2024(Even before the year)'
  END AS customer_type,
  COUNT(DISTINCT c.customer_id)  AS customer_count,
  COUNT(o.order_id)              AS order_count,
  SUM(o.quantity * o.unit_price) AS total_revenue,
  ROUND(SUM(o.quantity * o.unit_price) * 1.0
        / COUNT(o.order_id), 0)  AS avg_order_value
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY customer_type
ORDER BY customer_type DESC
''')

# (2) List of New Customers
print("\n② New customers (2024List of annual new contracts")
q(conn, '''
SELECT
  c.customer_id,
  c.customer_name,
  c.region,
  c.first_contract_date,
  COUNT(o.order_id)              AS order_count,
  SUM(o.quantity * o.unit_price) AS annual_revenue
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE c.first_contract_date >= '2024-01-01'
GROUP BY c.customer_id
ORDER BY c.first_contract_date
''')
[OUT: 8]
=== No.074 Classify new and existing customers ===

① new / existing Client Summary (2024 (Based on year)
── SQL ─────────────────────────────────────────
  SELECT
    CASE
      WHEN c.first_contract_date >= '2024-01-01' THEN 'New customers (2024New contracts every year)'
      ELSE 'Existing Customers (2024(Even before the year)'
    END AS customer_type,
    COUNT(DISTINCT c.customer_id)  AS customer_count,
    COUNT(o.order_id)              AS order_count,
    SUM(o.quantity * o.unit_price) AS total_revenue,
    ROUND(SUM(o.quantity * o.unit_price) * 1.0
          / COUNT(o.order_id), 0)  AS avg_order_value
  FROM customers c
  LEFT JOIN orders o ON c.customer_id = o.customer_id
  GROUP BY customer_type
  ORDER BY customer_type DESC
───────────────────────────────────────────────
shape: (2, 5)
┌─────────────────────────────────┬────────────────┬─────────────┬───────────────┬─────────────────┐
│ customer_type                   ┆ customer_count ┆ order_count ┆ total_revenue ┆ avg_order_value │
│ ---                             ┆ ---            ┆ ---         ┆ ---           ┆ ---             │
│ str                             ┆ i64            ┆ i64         ┆ i64           ┆ f64             │
╞═════════════════════════════════╪════════════════╪═════════════╪═══════════════╪═════════════════╡
│ New customers (2024New contrac… ┆ 4              ┆ 66          ┆ 11022295      ┆ 167004.0        │
│ Existing Customers (2024(Even … ┆ 16             ┆ 541         ┆ 97147411      ┆ 179570.0        │
└─────────────────────────────────┴────────────────┴─────────────┴───────────────┴─────────────────┘
↳ 2 rows retrieved

② New customers (2024List of annual new contracts
── SQL ─────────────────────────────────────────
  SELECT
    c.customer_id,
    c.customer_name,
    c.region,
    c.first_contract_date,
    COUNT(o.order_id)              AS order_count,
    SUM(o.quantity * o.unit_price) AS annual_revenue
  FROM customers c
  LEFT JOIN orders o ON c.customer_id = o.customer_id
  WHERE c.first_contract_date >= '2024-01-01'
  GROUP BY c.customer_id
  ORDER BY c.first_contract_date
───────────────────────────────────────────────
shape: (4, 6)
┌─────────────┬──────────────────────┬────────┬─────────────────────┬─────────────┬────────────────┐
│ customer_id ┆ customer_name        ┆ region ┆ first_contract_date ┆ order_count ┆ annual_revenue │
│ ---         ┆ ---                  ┆ ---    ┆ ---                 ┆ ---         ┆ ---            │
│ str         ┆ str                  ┆ str    ┆ str                 ┆ i64         ┆ i64            │
╞═════════════╪══════════════════════╪════════╪═════════════════════╪═════════════╪════════════════╡
│ C017        ┆ Hashimoto Machinery  ┆ Kanto  ┆ 2024-01-22          ┆ 23          ┆ 3634446        │
│ C008        ┆ Kobayashi Seiki      ┆ Kanto  ┆ 2024-02-15          ┆ 13          ┆ 1922514        │
│ C010        ┆ Yoshida Mechatronics ┆ Kanto  ┆ 2024-04-08          ┆ 22          ┆ 4206247        │
│ C013        ┆ Sato Machinery and   ┆ Kanto  ┆ 2024-07-03          ┆ 8           ┆ 1259088        │
│             ┆ Electronics…         ┆        ┆                     ┆             ┆                │
└─────────────┴──────────────────────┴────────┴─────────────────────┴─────────────┴────────────────┘
↳ 4 rows retrieved

Interpreting the results

  • New customers (first contracts of 2024) are 4 companies: C008 Kobayashi Seiki (February), C010 Yoshida Mechatronics (April), C017 Hashimoto Machinery (January), C013 Sato Kiden Kogyo (July)
  • New customers start transactions mid-year, so annual sales in the first year are often lower than those of existing customers
  • By comparing the average spend of new customers with existing customers, you can leverage it for Accuracy Verification of New Target Development purposes.

No.075: Calculating Repeat Rate

Practical use

Repeat Rate = Percentage of clients who placed orders more than twice within the year.
In manufacturing B2B, a higher repeat rate means a stable order base.
If you notice a downward trend in repeat rates, it is a sign of declining customer satisfaction or a shift to competitors.

How the SQL works

repeat rate=number of orders≥2 number of clientstotal number of customers×100\text{repeat rate} = \frac{\text{number of orders} \geq 2 \text{ number of clients}} {\text{total number of customers}} \times 100

You can combine CASE expressions and aggregation functions to compute in a single SQL:

SUM(CASE WHEN order_count >= 2 THEN 1 ELSE 0 END) * 100.0 / COUNT(*)

Run in Python

[IN: 9]
print("=== No.075 Calculating the repeat rate ===\n")

# (1) Overall Repeat Rate
print("① Overall repeat rate")
df75a = q(conn, '''
WITH order_counts AS (
  SELECT customer_id, COUNT(*) AS order_count
  FROM orders
  GROUP BY customer_id
)
SELECT
  COUNT(*)                                       AS total_customers,
  SUM(CASE WHEN order_count >= 2 THEN 1 ELSE 0 END) AS repeat_customers,
  SUM(CASE WHEN order_count  = 1 THEN 1 ELSE 0 END) AS one_time_customers,
  ROUND(SUM(CASE WHEN order_count >= 2 THEN 1 ELSE 0 END)
        * 100.0 / COUNT(*), 1)                  AS repeat_rate_pct
FROM order_counts
''')

# (2) Distribution of Client Numbers by Number of Orders
print("\n② Distribution of Client Numbers by Number of Orders")
q(conn, '''
WITH order_counts AS (
  SELECT customer_id, COUNT(*) AS order_count
  FROM orders
  GROUP BY customer_id
)
SELECT
  CASE
    WHEN order_count >= 30 THEN '30more than one'
    WHEN order_count >= 20 THEN '20〜29records'
    WHEN order_count >= 10 THEN '10〜19records'
    WHEN order_count >=  5 THEN '5〜9records'
    WHEN order_count >=  2 THEN '2〜4records'
    ELSE '1only case'
  END AS freq_band,
  COUNT(*) AS customer_count
FROM order_counts
GROUP BY freq_band
ORDER BY MIN(order_count) DESC
''')
[OUT: 9]
=== No.075 Calculating the repeat rate ===

① Overall repeat rate
── SQL ─────────────────────────────────────────
  WITH order_counts AS (
    SELECT customer_id, COUNT(*) AS order_count
    FROM orders
    GROUP BY customer_id
  )
  SELECT
    COUNT(*)                                       AS total_customers,
    SUM(CASE WHEN order_count >= 2 THEN 1 ELSE 0 END) AS repeat_customers,
    SUM(CASE WHEN order_count  = 1 THEN 1 ELSE 0 END) AS one_time_customers,
    ROUND(SUM(CASE WHEN order_count >= 2 THEN 1 ELSE 0 END)
          * 100.0 / COUNT(*), 1)                  AS repeat_rate_pct
  FROM order_counts
───────────────────────────────────────────────
shape: (1, 4)
┌─────────────────┬──────────────────┬────────────────────┬─────────────────┐
│ total_customers ┆ repeat_customers ┆ one_time_customers ┆ repeat_rate_pct │
│ ---             ┆ ---              ┆ ---                ┆ ---             │
│ i64             ┆ i64              ┆ i64                ┆ f64             │
╞═════════════════╪══════════════════╪════════════════════╪═════════════════╡
│ 20              ┆ 20               ┆ 0                  ┆ 100.0           │
└─────────────────┴──────────────────┴────────────────────┴─────────────────┘
↳ 1 rows retrieved

② Distribution of Client Numbers by Number of Orders
── SQL ─────────────────────────────────────────
  WITH order_counts AS (
    SELECT customer_id, COUNT(*) AS order_count
    FROM orders
    GROUP BY customer_id
  )
  SELECT
    CASE
      WHEN order_count >= 30 THEN '30more than one'
      WHEN order_count >= 20 THEN '20〜29records'
      WHEN order_count >= 10 THEN '10〜19records'
      WHEN order_count >=  5 THEN '5〜9records'
      WHEN order_count >=  2 THEN '2〜4records'
      ELSE '1only case'
    END AS freq_band,
    COUNT(*) AS customer_count
  FROM order_counts
  GROUP BY freq_band
  ORDER BY MIN(order_count) DESC
───────────────────────────────────────────────
shape: (4, 2)
┌─────────────────┬────────────────┐
│ freq_band       ┆ customer_count │
│ ---             ┆ ---            │
│ str             ┆ i64            │
╞═════════════════╪════════════════╡
│ 30more than one ┆ 9              │
│ 20〜29records   ┆ 7              │
│ 10〜19records   ┆ 3              │
│ 5〜9records     ┆ 1              │
└─────────────────┴────────────────┘
↳ 4 rows retrieved

Interpreting the results

  • Check the repeat-order rate. A high rate alone does not establish satisfaction or relationship quality; also examine contract periods and ordering intervals.
  • If you have many clients with “just one deal,” Connecting trial orders to ongoing orders follow-up is necessary.
  • By tracking the annual repeat rate trends, it can be used as a Customer Loyalty Metrics for management decision-making.

No.076: Calculating the Last Purchase Date

Practical use

Number of days elapsed since the last order date (Recency) is the “R” component in RFM analysis.
Clients who have not placed orders for a long time are treated as dormant customer and become targets for sales follow-up.

How the SQL works

In SQLite, the julianday() function converts the date to floating-point (Julian Day),
By taking the difference, you can calculate the number of days elapsed.

elapsed days=julianday(’2024-12-31’)−julianday(MAX(order_date))\text{elapsed days} = \text{julianday}(\text{'2024-12-31'}) - \text{julianday}(\text{MAX(order\_date)})
Number of days elapsedCategoriesResponse
0–30 daysActiveSuggestions for the Next Order
31–90 daysNoteFollow-up Contact
91 days or moredormant candidateNeed to rebuild relationships

Run in Python

[IN: 10]
print("=== No.076 Calculate the last purchase date ===\n")

# (1) Last Order Date and Number of Days Passed
print("① Departing in triumph Last Order Date (in order of elapsed days)")
df76 = q(conn, '''
SELECT
  o.customer_id,
  c.customer_name,
  c.region,
  MAX(o.order_date)                                           AS last_order_date,
  ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS days_since_last,
  CASE
    WHEN julianday('2024-12-31') - julianday(MAX(o.order_date)) <= 30 THEN 'Active'
    WHEN julianday('2024-12-31') - julianday(MAX(o.order_date)) <= 90 THEN 'Note'
    ELSE 'dormant candidate'
  END AS recency_status
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY o.customer_id
ORDER BY days_since_last DESC
''')

# (2) Status Summary
print("\n② By Status of Number of Days Passed Since Last Order Proud and quick to count")
q(conn, '''
WITH recency AS (
  SELECT
    customer_id,
    ROUND(julianday('2024-12-31') - julianday(MAX(order_date)), 0) AS days_since_last
  FROM orders
  GROUP BY customer_id
)
SELECT
  CASE
    WHEN days_since_last <= 30 THEN 'Active (30within a few days)'
    WHEN days_since_last <= 90 THEN 'Note (31〜90Day)'
    ELSE 'Dormant candidate (91(days)'
  END AS recency_status,
  COUNT(*) AS customer_count
FROM recency
GROUP BY recency_status
ORDER BY MIN(days_since_last)
''')
[OUT: 10]
=== No.076 Calculate the last purchase date ===

① Departing in triumph Last Order Date (in order of elapsed days)
── SQL ─────────────────────────────────────────
  SELECT
    o.customer_id,
    c.customer_name,
    c.region,
    MAX(o.order_date)                                           AS last_order_date,
    ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS days_since_last,
    CASE
      WHEN julianday('2024-12-31') - julianday(MAX(o.order_date)) <= 30 THEN 'Active'
      WHEN julianday('2024-12-31') - julianday(MAX(o.order_date)) <= 90 THEN 'Note'
      ELSE 'dormant candidate'
    END AS recency_status
  FROM orders o
  JOIN customers c ON o.customer_id = c.customer_id
  GROUP BY o.customer_id
  ORDER BY days_since_last DESC
───────────────────────────────────────────────
shape: (20, 6)
┌─────────────┬────────────────────┬──────────┬─────────────────┬─────────────────┬────────────────┐
│ customer_id ┆ customer_name      ┆ region   ┆ last_order_date ┆ days_since_last ┆ recency_status │
│ ---         ┆ ---                ┆ ---      ┆ ---             ┆ ---             ┆ ---            │
│ str         ┆ str                ┆ str      ┆ str             ┆ f64             ┆ str            │
╞═════════════╪════════════════════╪══════════╪═════════════════╪═════════════════╪════════════════╡
│ C012        ┆ Matsumoto          ┆ Kyushu   ┆ 2024-11-04      ┆ 57.0            ┆ Note           │
│             ┆ Manufacturing      ┆          ┆                 ┆                 ┆                │
│ C013        ┆ Sato Machinery and ┆ Kanto    ┆ 2024-11-22      ┆ 39.0            ┆ Note           │
│             ┆ Electronics…       ┆          ┆                 ┆                 ┆                │
│ C015        ┆ Ichikawa Kogyo     ┆ East Sea ┆ 2024-12-07      ┆ 24.0            ┆ Active         │
│             ┆ Co., Ltd.          ┆          ┆                 ┆                 ┆                │
│ C010        ┆ Yoshida            ┆ Kanto    ┆ 2024-12-15      ┆ 16.0            ┆ Active         │
│             ┆ Mechatronics       ┆          ┆                 ┆                 ┆                │
│ C006        ┆ Watanabe Machinery ┆ Kyushu   ┆ 2024-12-18      ┆ 13.0            ┆ Active         │
│             ┆ Industry           ┆          ┆                 ┆                 ┆                │
│ …           ┆ …                  ┆ …        ┆ …               ┆ …               ┆ …              │
│ C007        ┆ Nakamura           ┆ East Sea ┆ 2024-12-25      ┆ 6.0             ┆ Active         │
│             ┆ Manufacturing      ┆          ┆                 ┆                 ┆                │
│ C016        ┆ Kimura             ┆ Kyushu   ┆ 2024-12-26      ┆ 5.0             ┆ Active         │
│             ┆ Manufacturing      ┆          ┆                 ┆                 ┆                │
│ C005        ┆ Ito Kogyo Co.,     ┆ Kansai   ┆ 2024-12-26      ┆ 5.0             ┆ Active         │
│             ┆ Ltd.               ┆          ┆                 ┆                 ┆                │
│ C017        ┆ Hashimoto          ┆ Kanto    ┆ 2024-12-27      ┆ 4.0             ┆ Active         │
│             ┆ Machinery          ┆          ┆                 ┆                 ┆                │
│ C002        ┆ Tanaka Steel Co.,  ┆ Kansai   ┆ 2024-12-27      ┆ 4.0             ┆ Active         │
│             ┆ Ltd.               ┆          ┆                 ┆                 ┆                │
└─────────────┴────────────────────┴──────────┴─────────────────┴─────────────────┴────────────────┘
↳ 20 rows retrieved

② By Status of Number of Days Passed Since Last Order Proud and quick to count
── SQL ─────────────────────────────────────────
  WITH recency AS (
    SELECT
      customer_id,
      ROUND(julianday('2024-12-31') - julianday(MAX(order_date)), 0) AS days_since_last
    FROM orders
    GROUP BY customer_id
  )
  SELECT
    CASE
      WHEN days_since_last <= 30 THEN 'Active (30within a few days)'
      WHEN days_since_last <= 90 THEN 'Note (31〜90Day)'
      ELSE 'Dormant candidate (91(days)'
    END AS recency_status,
    COUNT(*) AS customer_count
  FROM recency
  GROUP BY recency_status
  ORDER BY MIN(days_since_last)
───────────────────────────────────────────────
shape: (2, 2)
┌──────────────────────────────┬────────────────┐
│ recency_status               ┆ customer_count │
│ ---                          ┆ ---            │
│ str                          ┆ i64            │
╞══════════════════════════════╪════════════════╡
│ Active (30within a few days) ┆ 18             │
│ Note (31〜90Day)             ┆ 2              │
└──────────────────────────────┴────────────────┘
↳ 2 rows retrieved

Interpreting the results

  • Flag customers with at least 91 days since their last order.
  • The reference date here is December 31, 2024. For scheduled monitoring, update it and use only orders on or before that date.
  • Include annual order value to help prioritize follow-up.

No.077: Creating Data for RFM Analysis

Practical use

RFM Analysis is a customer segment analysis method that scores customers based on three indicators.

indicatorEnglishDefinitionMeaning in manufacturing
RRencyNumber of days elapsed since final orderHave you recently placed an order?
FFrequencyNumber of orders receivedHow many times have you placed an order?
MMonetary (amount)Total Order AmountHow much are you buying?

Customers with high RFM scores (RFM = 3-3-3) have Most Important Customers (VIP), while customers with low scores (1-1-1) have Risk of Defection.

How the SQL works

Convert each indicator into a score (1–3):

Rscore={3R≤30,230<R≤90,1R>90.R_{\mathrm{score}}=\begin{cases} 3 & R\leq30,\\2 & 30<R\leq90,\\1 & R>90. \end{cases} Fscore={3F≥25,210≤F<25,1F<10.F_{\mathrm{score}}=\begin{cases} 3 & F\geq25,\\2 & 10\leq F<25,\\1 & F<10. \end{cases} Mscore={3M≥2,000,000,2800,000≤M<2,000,000,1M<800,000.M_{\mathrm{score}}=\begin{cases} 3 & M\geq2{,}000{,}000,\\ 2 & 800{,}000\leq M<2{,}000{,}000,\\ 1 & M<800{,}000. \end{cases}

RR is days since the last order, FF is order count, and MM is order value in yen. The thresholds are illustrative.

Run in Python

[IN: 11]
print("=== No.077 RFMCreate data for analysis ===\n")

# (1) RFM Data Creation
print("① RFM Score Table")
df77 = q(conn, '''
WITH rfm_raw AS (
  SELECT
    o.customer_id,
    c.customer_name,
    ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS recency,
    COUNT(o.order_id)              AS frequency,
    SUM(o.quantity * o.unit_price) AS monetary
  FROM orders o
  JOIN customers c ON o.customer_id = c.customer_id
  GROUP BY o.customer_id
)
SELECT
  customer_id,
  customer_name,
  recency,
  frequency,
  monetary,
  CASE WHEN recency  <=  30 THEN 3 WHEN recency  <=  90 THEN 2 ELSE 1 END AS r_score,
  CASE WHEN frequency >= 25 THEN 3 WHEN frequency >= 10  THEN 2 ELSE 1 END AS f_score,
  CASE WHEN monetary >= 2000000 THEN 3 WHEN monetary >= 800000 THEN 2 ELSE 1 END AS m_score
FROM rfm_raw
ORDER BY monetary DESC
''')

# (2) RFM Segment Aggregation
print("\n② RFM Segment aggregation")
df77s = q(conn, '''
WITH rfm_raw AS (
  SELECT
    o.customer_id,
    ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS recency,
    COUNT(o.order_id)              AS frequency,
    SUM(o.quantity * o.unit_price) AS monetary
  FROM orders o
  GROUP BY o.customer_id
),
scored AS (
  SELECT
    customer_id,
    CASE WHEN recency  <=  30 THEN 3 WHEN recency  <=  90 THEN 2 ELSE 1 END AS r,
    CASE WHEN frequency >= 25 THEN 3 WHEN frequency >= 10  THEN 2 ELSE 1 END AS f,
    CASE WHEN monetary >= 2000000 THEN 3 WHEN monetary >= 800000 THEN 2 ELSE 1 END AS m
  FROM rfm_raw
)
SELECT
  r || '-' || f || '-' || m AS rfm_segment,
  CASE
    WHEN r=3 AND f=3 AND m=3 THEN 'VIPcustomer'
    WHEN r=1 AND f<=2 AND m<=2 THEN 'Risk of Defection'
    WHEN r>=2 AND f>=2 THEN 'Excellent Customer'
    ELSE 'Other'
  END AS segment_label,
  COUNT(*) AS customer_count
FROM scored
GROUP BY rfm_segment
ORDER BY rfm_segment DESC
''')

# Visualization: F vs M scatter plot (color = R score)
R_COLORS = {1: '#e74c3c', 2: '#f39c12', 3: '#2ecc71'}
fig, ax = plt.subplots(figsize=(9, 6))
for r_val in [3, 2, 1]:
    sub = df77.filter(pl.col('r_score') == r_val)
    ax.scatter(
        sub['frequency'].to_list(),
        [v / 1_000_000 for v in sub['monetary'].to_list()],
        color=R_COLORS[r_val],
        label=f'R={r_val}({"Most recent active" if r_val==3 else "Note" if r_val==2 else "dormant candidate"})',
        s=80, alpha=0.8, edgecolors='white', linewidth=0.5
    )
ax.set_title('RFM Analysis: Order Frequency vs Sales amount (color) = Recency Score)', fontsize=13)
ax.set_xlabel('Number of orders (Frequency)')
ax.set_ylabel('Annual sales amount (million yen)')
ax.legend(fontsize=9)
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 11]
=== No.077 RFMCreate data for analysis ===

① RFM Score Table
── SQL ─────────────────────────────────────────
  WITH rfm_raw AS (
    SELECT
      o.customer_id,
      c.customer_name,
      ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS recency,
      COUNT(o.order_id)              AS frequency,
      SUM(o.quantity * o.unit_price) AS monetary
    FROM orders o
    JOIN customers c ON o.customer_id = c.customer_id
    GROUP BY o.customer_id
  )
  SELECT
    customer_id,
    customer_name,
    recency,
    frequency,
    monetary,
    CASE WHEN recency  <=  30 THEN 3 WHEN recency  <=  90 THEN 2 ELSE 1 END AS r_score,
    CASE WHEN frequency >= 25 THEN 3 WHEN frequency >= 10  THEN 2 ELSE 1 END AS f_score,
    CASE WHEN monetary >= 2000000 THEN 3 WHEN monetary >= 800000 THEN 2 ELSE 1 END AS m_score
  FROM rfm_raw
  ORDER BY monetary DESC
───────────────────────────────────────────────
shape: (20, 8)
┌─────────────┬─────────────────────┬─────────┬───────────┬──────────┬─────────┬─────────┬─────────┐
│ customer_id ┆ customer_name       ┆ recency ┆ frequency ┆ monetary ┆ r_score ┆ f_score ┆ m_score │
│ ---         ┆ ---                 ┆ ---     ┆ ---       ┆ ---      ┆ ---     ┆ ---     ┆ ---     │
│ str         ┆ str                 ┆ f64     ┆ i64       ┆ i64      ┆ i64     ┆ i64     ┆ i64     │
╞═════════════╪═════════════════════╪═════════╪═══════════╪══════════╪═════════╪═════════╪═════════╡
│ C001        ┆ Yamada Machinery    ┆ 8.0     ┆ 58        ┆ 10020852 ┆ 3       ┆ 3       ┆ 3       │
│             ┆ Works               ┆         ┆           ┆          ┆         ┆         ┆         │
│ C005        ┆ Ito Kogyo Co., Ltd. ┆ 5.0     ┆ 48        ┆ 9205481  ┆ 3       ┆ 3       ┆ 3       │
│ C007        ┆ Nakamura            ┆ 6.0     ┆ 47        ┆ 9068969  ┆ 3       ┆ 3       ┆ 3       │
│             ┆ Manufacturing       ┆         ┆           ┆          ┆         ┆         ┆         │
│ C014        ┆ Shimizu Seiko       ┆ 7.0     ┆ 44        ┆ 8495483  ┆ 3       ┆ 3       ┆ 3       │
│ C003        ┆ Suzuki Precision    ┆ 11.0    ┆ 44        ┆ 7915003  ┆ 3       ┆ 3       ┆ 3       │
│             ┆ Industry            ┆         ┆           ┆          ┆         ┆         ┆         │
│ …           ┆ …                   ┆ …       ┆ …         ┆ …        ┆ …       ┆ …       ┆ …       │
│ C020        ┆ Fujita              ┆ 11.0    ┆ 21        ┆ 3398339  ┆ 3       ┆ 2       ┆ 3       │
│             ┆ Manufacturing       ┆         ┆           ┆          ┆         ┆         ┆         │
│ C016        ┆ Kimura              ┆ 5.0     ┆ 16        ┆ 2648905  ┆ 3       ┆ 2       ┆ 3       │
│             ┆ Manufacturing       ┆         ┆           ┆          ┆         ┆         ┆         │
│ C012        ┆ Matsumoto           ┆ 57.0    ┆ 10        ┆ 2080769  ┆ 2       ┆ 2       ┆ 3       │
│             ┆ Manufacturing       ┆         ┆           ┆          ┆         ┆         ┆         │
│ C008        ┆ Kobayashi Seiki     ┆ 9.0     ┆ 13        ┆ 1922514  ┆ 3       ┆ 2       ┆ 2       │
│ C013        ┆ Sato Machinery and  ┆ 39.0    ┆ 8         ┆ 1259088  ┆ 2       ┆ 1       ┆ 2       │
│             ┆ Electronics…        ┆         ┆           ┆          ┆         ┆         ┆         │
└─────────────┴─────────────────────┴─────────┴───────────┴──────────┴─────────┴─────────┴─────────┘
↳ 20 rows retrieved

② RFM Segment aggregation
── SQL ─────────────────────────────────────────
  WITH rfm_raw AS (
    SELECT
      o.customer_id,
      ROUND(julianday('2024-12-31') - julianday(MAX(o.order_date)), 0) AS recency,
      COUNT(o.order_id)              AS frequency,
      SUM(o.quantity * o.unit_price) AS monetary
    FROM orders o
    GROUP BY o.customer_id
  ),
  scored AS (
    SELECT
      customer_id,
      CASE WHEN recency  <=  30 THEN 3 WHEN recency  <=  90 THEN 2 ELSE 1 END AS r,
      CASE WHEN frequency >= 25 THEN 3 WHEN frequency >= 10  THEN 2 ELSE 1 END AS f,
      CASE WHEN monetary >= 2000000 THEN 3 WHEN monetary >= 800000 THEN 2 ELSE 1 END AS m
    FROM rfm_raw
  )
  SELECT
    r || '-' || f || '-' || m AS rfm_segment,
    CASE
      WHEN r=3 AND f=3 AND m=3 THEN 'VIPcustomer'
      WHEN r=1 AND f<=2 AND m<=2 THEN 'Risk of Defection'
      WHEN r>=2 AND f>=2 THEN 'Excellent Customer'
      ELSE 'Other'
    END AS segment_label,
    COUNT(*) AS customer_count
  FROM scored
  GROUP BY rfm_segment
  ORDER BY rfm_segment DESC
───────────────────────────────────────────────
shape: (5, 3)
┌─────────────┬────────────────────┬────────────────┐
│ rfm_segment ┆ segment_label      ┆ customer_count │
│ ---         ┆ ---                ┆ ---            │
│ str         ┆ str                ┆ i64            │
╞═════════════╪════════════════════╪════════════════╡
│ 3-3-3       ┆ VIPcustomer        ┆ 11             │
│ 3-2-3       ┆ Excellent Customer ┆ 6              │
│ 3-2-2       ┆ Excellent Customer ┆ 1              │
│ 2-2-3       ┆ Excellent Customer ┆ 1              │
│ 2-1-2       ┆ Other              ┆ 1              │
└─────────────┴────────────────────┴────────────────┘
↳ 5 rows retrieved

Chart 1 for input 11

Interpreting the results

  • If the dot in the upper right (high F / high M) and green (R=3) is VIP customer: frequent and large transactions until recently
  • If the lower left (low F, low M) and red dot (R=1) are Defection risk customers: orders have stopped.
  • RFM scores can be regularly recalculated to track Changes in the customer portfolio
    → Taking early sales actions for customers whose segments are deteriorating

No.078: Comparing inventory and sales volume

Practical use

By understanding “how many months’ worth of demand does the current inventory amount correspond to?“,
You can manage both Excess inventory (cash flow pressure) and Inventory shortage (missed opportunity) risks.

How the SQL works

Use order quantities from October–December as a demand proxy to calculate monthly demand and months of stock. These are not shipment records and do not account for cancellations or backlogs.

Months in stock=Current inventory quantityAverage monthly demand\text{Months in stock} = \frac{\text{Current inventory quantity}}{\text{Average monthly demand}}
Inventory monthsReceptionResponse
≥ 6 monthsexcess inventoryProduction Plan Review & Bargain Sale
2 ≤ months < 6Adequate in this exerciseCheck demand and lead time
1 ≤ months < 2CautionConsider replenishment
< 1 monthdangerous areaEmergency orders and alternative arrangements

Run in Python

[IN: 12]
print("=== No.078 Compare inventory and sales numbers ===\n")

# (1) Inventory vs. Shipment Performance in the Past 3 Months
print("① By product Inventory vs most recent3ヶMonthly demand (by number of months in stock)")
df78 = q(conn, '''
WITH recent_demand AS (
  SELECT
    product_id,
    SUM(quantity)                    AS qty_3m,
    SUM(quantity) * 1.0 / 3 AS monthly_avg_qty
  FROM orders
  WHERE order_date >= '2024-10-01' AND order_date < '2025-01-01'
  GROUP BY product_id
)
SELECT
  p.product_id,
  p.product_name,
  p.category,
  i.stock_qty,
  i.safety_stock,
  COALESCE(d.monthly_avg_qty, 0) AS monthly_avg_qty,
  CASE
    WHEN COALESCE(d.monthly_avg_qty, 0) = 0 THEN 999
    ELSE ROUND(i.stock_qty * 1.0 / d.monthly_avg_qty, 1)
  END AS stock_months
FROM products p
JOIN inventory i ON p.product_id = i.product_id
LEFT JOIN recent_demand d ON p.product_id = d.product_id
ORDER BY stock_months ASC
''')

# (2) Visualization: Inventory vs. Safety Stock (Bar Graph)
names   = df78['product_name'].to_list()
stocks  = df78['stock_qty'].to_list()
safetys = df78['safety_stock'].to_list()
months  = df78['stock_months'].to_list()
bar_colors = ['#e74c3c' if s < sf else '#f39c12' if s < sf * 2 else '#2ecc71'
              for s, sf in zip(stocks, safetys)]

x = range(len(names))
fig, ax = plt.subplots(figsize=(11, 5))
ax.bar(x, stocks,  color=bar_colors, label='Current Warehouse', edgecolor='white', alpha=0.9)
ax.plot(x, safetys, 's--', color='#e74c3c', markersize=8, label='Safety stock line', linewidth=1.5)
ax.set_title('By product Current Warehouse vs Safety stock (No.078)', fontsize=13)
ax.set_xlabel('Products')
ax.set_ylabel('Inventory quantity')
ax.set_xticks(list(x))
ax.set_xticklabels(names, rotation=30, ha='right')
ax.legend(fontsize=10)
ax.grid(axis='y', alpha=0.4)
plt.tight_layout()
plt.show()
[OUT: 12]
=== No.078 Compare inventory and sales numbers ===

① By product Inventory vs most recent3ヶMonthly demand (by number of months in stock)
── SQL ─────────────────────────────────────────
  WITH recent_demand AS (
    SELECT
      product_id,
      SUM(quantity)                    AS qty_3m,
      SUM(quantity) * 1.0 / 3 AS monthly_avg_qty
    FROM orders
    WHERE order_date >= '2024-10-01' AND order_date < '2025-01-01'
    GROUP BY product_id
  )
  SELECT
    p.product_id,
    p.product_name,
    p.category,
    i.stock_qty,
    i.safety_stock,
    COALESCE(d.monthly_avg_qty, 0) AS monthly_avg_qty,
    CASE
      WHEN COALESCE(d.monthly_avg_qty, 0) = 0 THEN 999
      ELSE ROUND(i.stock_qty * 1.0 / d.monthly_avg_qty, 1)
    END AS stock_months
  FROM products p
  JOIN inventory i ON p.product_id = i.product_id
  LEFT JOIN recent_demand d ON p.product_id = d.product_id
  ORDER BY stock_months ASC
───────────────────────────────────────────────
shape: (10, 7)
┌────────────┬──────────────┬──────────────┬───────────┬──────────────┬──────────────┬─────────────┐
│ product_id ┆ product_name ┆ category     ┆ stock_qty ┆ safety_stock ┆ monthly_avg_ ┆ stock_month │
│ ---        ┆ ---          ┆ ---          ┆ ---       ┆ ---          ┆ qty          ┆ s           │
│ str        ┆ str          ┆ str          ┆ i64       ┆ i64          ┆ ---          ┆ ---         │
│            ┆              ┆              ┆           ┆              ┆ f64          ┆ f64         │
╞════════════╪══════════════╪══════════════╪═══════════╪══════════════╪══════════════╪═════════════╡
│ P001       ┆ Precision    ┆ Machine      ┆ 850       ┆ 300          ┆ 732.333333   ┆ 1.2         │
│            ┆ Bearings     ┆ parts        ┆           ┆              ┆              ┆             │
│ P005       ┆ Industrial   ┆ consumables  ┆ 5000      ┆ 1500         ┆ 4171.333333  ┆ 1.2         │
│            ┆ Rubber       ┆              ┆           ┆              ┆              ┆             │
│            ┆ Packing      ┆              ┆           ┆              ┆              ┆             │
│ P003       ┆ motor coil   ┆ electrical   ┆ 380       ┆ 200          ┆ 275.0        ┆ 1.4         │
│            ┆              ┆ components   ┆           ┆              ┆              ┆             │
│ P009       ┆ rolling      ┆ Machine      ┆ 450       ┆ 300          ┆ 316.666667   ┆ 1.4         │
│            ┆ bearing      ┆ parts        ┆           ┆              ┆              ┆             │
│ P008       ┆ heat-resista ┆ consumables  ┆ 3200      ┆ 1000         ┆ 2066.333333  ┆ 1.5         │
│            ┆ nt gasket    ┆              ┆           ┆              ┆              ┆             │
│ P002       ┆ hydraulic    ┆ Machine      ┆ 120       ┆ 150          ┆ 69.333333    ┆ 1.7         │
│            ┆ cylinder     ┆ parts        ┆           ┆              ┆              ┆             │
│ P010       ┆ electromagne ┆ electrical   ┆ 65        ┆ 80           ┆ 34.333333    ┆ 1.9         │
│            ┆ tic valve    ┆ components   ┆           ┆              ┆              ┆             │
│ P007       ┆ three-phase  ┆ electrical   ┆ 8         ┆ 15           ┆ 3.666667     ┆ 2.2         │
│            ┆ motor        ┆ components   ┆           ┆              ┆              ┆             │
│ P006       ┆ Stainless    ┆ Machine      ┆ 280       ┆ 150          ┆ 95.666667    ┆ 2.9         │
│            ┆ steel flange ┆ parts        ┆           ┆              ┆              ┆             │
│ P004       ┆ Control      ┆ electrical   ┆ 25        ┆ 40           ┆ 6.333333     ┆ 3.9         │
│            ┆ board        ┆ components   ┆           ┆              ┆              ┆             │
└────────────┴──────────────┴──────────────┴───────────┴──────────────┴──────────────┴─────────────┘
↳ 10 rows retrieved

Chart 1 for input 12

Interpreting the results

  • Red stick of the graph (Current Inventory < Safety Stock) are products at risk of being out of stock (detailed analysis in → No.079).
  • The Orange bar in the graph (safety stock up to 2 times) indicates products that require consideration for replenishment.
  • Products with high stock_months (such as consumables like P005 packing) have room to reduce Storage costs due to excess inventory

No.079: Extracting Products at Risk of Out-of-Stock

Practical use

Stockouts lead to Missed opportunities, production stoppages, and inconvenience to clients.
We calculate the Remaining Inventory Days (Runout Days) considering the order lead time,
It is important to automatically alert for products that require replenishment orders.

How the SQL works

Days remaining in stock=Current stockaverage daily demand=stock_qtymonthly_avg_qty/30\text{Days remaining in stock} = \frac{\text{Current stock}}{\text{average daily demand}} = \frac{\text{stock\_qty}}{\text{monthly\_avg\_qty} / 30}

For products with a lead time of LL days:

Condition to be supplemented:Remaining stock days<L⋅1.5(Applies safety factor 1.5)\text{Condition to be supplemented} :\quad \text{Remaining stock days} < L \cdot 1.5 \quad (\text{Applies safety factor 1.5})

The lead-time threshold is an additional check. The stock_status below is based on stock relative to safety stock. With zero observed demand, days of stock is undefined and returned as NULL.

Run in Python

[IN: 13]
print("=== No.079 Extracting products at risk of out-of-stock ===\n")

# (1) Inventory Risk Assessment
print("① Inventory Risk Assessment (Remaining Inventory Days) + Status)")
df79 = q(conn, '''
WITH recent_demand AS (
  SELECT
    product_id,
    SUM(quantity) * 1.0 / 3 AS monthly_avg_qty
  FROM orders
  WHERE order_date >= '2024-10-01' AND order_date < '2025-01-01'
  GROUP BY product_id
),
stock_eval AS (
  SELECT
    i.product_id,
    i.stock_qty,
    i.safety_stock,
    p.product_name,
    p.lead_time_days,
    COALESCE(d.monthly_avg_qty, 0) AS monthly_avg_qty,
    CASE
      WHEN COALESCE(d.monthly_avg_qty, 0) = 0 THEN NULL
      ELSE ROUND(i.stock_qty * 30.0 / d.monthly_avg_qty, 0)
    END AS runout_days,
    CASE
      WHEN i.stock_qty < i.safety_stock THEN 'Stockout risk'
      WHEN i.stock_qty < i.safety_stock * 2 THEN 'To add,'
      ELSE 'Ample stock'
    END AS stock_status
  FROM inventory i
  JOIN products p ON i.product_id = p.product_id
  LEFT JOIN recent_demand d ON i.product_id = d.product_id
)
SELECT * FROM stock_eval ORDER BY runout_days ASC
''')

# (2) Display only risk items
print("\n② Stockout risk / To add, item")
risk = df79.filter(pl.col('stock_status').is_in(['Stockout risk', 'To add,']))
for row in risk.iter_rows(named=True):
    days_label = f"{row['runout_days']:.0f}" if row['runout_days'] is not None else 'N/A'
    print(f"  {row['product_id']} {row['product_name']:18s} "
          f"Inventory:{row['stock_qty']:5d} Safety stock:{row['safety_stock']:5d} "
          f"remnant:{days_label:>4}days LT:{row['lead_time_days']}days [{row['stock_status']}]")

# Visualization: Days remaining inventory (Gantbar style)
STATUS_COLORS = {'Stockout risk': '#e74c3c', 'To add,': '#f39c12', 'Ample stock': '#2ecc71'}
names79   = df79['product_name'].to_list()
runouts   = [(min(r, 180) if r is not None else float("nan")) for r in df79['runout_days'].to_list()]
statuses  = df79['stock_status'].to_list()
bar_cols  = [STATUS_COLORS[s] for s in statuses]
lead_days = df79['lead_time_days'].to_list()

fig, ax = plt.subplots(figsize=(10, 5))
ax.barh(names79, runouts, color=bar_cols, edgecolor='white')
for i, (ld, nm) in enumerate(zip(lead_days, names79)):
    ax.axvline(ld * 1.5, color='gray', linestyle=':', linewidth=0.8, alpha=0.5)
ax.axvline(30, color='#e74c3c', linestyle='--', linewidth=1.5, label='30Day Line (Alert)')
ax.set_title('By product Remaining Inventory Days (No.079: Out-of-stock risk assessment)', fontsize=12)
ax.set_xlabel('Remaining Inventory Days')
ax.set_ylabel('Products')
ax.legend(fontsize=9)
ax.grid(axis='x', alpha=0.4)
plt.tight_layout()
plt.show()
[OUT: 13]
=== No.079 Extracting products at risk of out-of-stock ===

① Inventory Risk Assessment (Remaining Inventory Days) + Status)
── SQL ─────────────────────────────────────────
  WITH recent_demand AS (
    SELECT
      product_id,
      SUM(quantity) * 1.0 / 3 AS monthly_avg_qty
    FROM orders
    WHERE order_date >= '2024-10-01' AND order_date < '2025-01-01'
    GROUP BY product_id
  ),
  stock_eval AS (
    SELECT
      i.product_id,
      i.stock_qty,
      i.safety_stock,
      p.product_name,
      p.lead_time_days,
      COALESCE(d.monthly_avg_qty, 0) AS monthly_avg_qty,
      CASE
        WHEN COALESCE(d.monthly_avg_qty, 0) = 0 THEN NULL
        ELSE ROUND(i.stock_qty * 30.0 / d.monthly_avg_qty, 0)
      END AS runout_days,
      CASE
        WHEN i.stock_qty < i.safety_stock THEN 'Stockout risk'
        WHEN i.stock_qty < i.safety_stock * 2 THEN 'To add,'
        ELSE 'Ample stock'
      END AS stock_status
    FROM inventory i
    JOIN products p ON i.product_id = p.product_id
    LEFT JOIN recent_demand d ON i.product_id = d.product_id
  )
  SELECT * FROM stock_eval ORDER BY runout_days ASC
───────────────────────────────────────────────
shape: (10, 8)
┌────────────┬───────────┬────────────┬────────────┬───────────┬───────────┬───────────┬───────────┐
│ product_id ┆ stock_qty ┆ safety_sto ┆ product_na ┆ lead_time ┆ monthly_a ┆ runout_da ┆ stock_sta │
│ ---        ┆ ---       ┆ ck         ┆ me         ┆ _days     ┆ vg_qty    ┆ ys        ┆ tus       │
│ str        ┆ i64       ┆ ---        ┆ ---        ┆ ---       ┆ ---       ┆ ---       ┆ ---       │
│            ┆           ┆ i64        ┆ str        ┆ i64       ┆ f64       ┆ f64       ┆ str       │
╞════════════╪═══════════╪════════════╪════════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ P001       ┆ 850       ┆ 300        ┆ Precision  ┆ 7         ┆ 732.33333 ┆ 35.0      ┆ Ample     │
│            ┆           ┆            ┆ Bearings   ┆           ┆ 3         ┆           ┆ stock     │
│ P005       ┆ 5000      ┆ 1500       ┆ Industrial ┆ 3         ┆ 4171.3333 ┆ 36.0      ┆ Ample     │
│            ┆           ┆            ┆ Rubber     ┆           ┆ 33        ┆           ┆ stock     │
│            ┆           ┆            ┆ Packing    ┆           ┆           ┆           ┆           │
│ P003       ┆ 380       ┆ 200        ┆ motor coil ┆ 5         ┆ 275.0     ┆ 41.0      ┆ To add,   │
│ P009       ┆ 450       ┆ 300        ┆ rolling    ┆ 5         ┆ 316.66666 ┆ 43.0      ┆ To add,   │
│            ┆           ┆            ┆ bearing    ┆           ┆ 7         ┆           ┆           │
│ P008       ┆ 3200      ┆ 1000       ┆ heat-resis ┆ 3         ┆ 2066.3333 ┆ 46.0      ┆ Ample     │
│            ┆           ┆            ┆ tant       ┆           ┆ 33        ┆           ┆ stock     │
│            ┆           ┆            ┆ gasket     ┆           ┆           ┆           ┆           │
│ P002       ┆ 120       ┆ 150        ┆ hydraulic  ┆ 10        ┆ 69.333333 ┆ 52.0      ┆ Stockout  │
│            ┆           ┆            ┆ cylinder   ┆           ┆           ┆           ┆ risk      │
│ P010       ┆ 65        ┆ 80         ┆ electromag ┆ 10        ┆ 34.333333 ┆ 57.0      ┆ Stockout  │
│            ┆           ┆            ┆ netic      ┆           ┆           ┆           ┆ risk      │
│            ┆           ┆            ┆ valve      ┆           ┆           ┆           ┆           │
│ P007       ┆ 8         ┆ 15         ┆ three-phas ┆ 14        ┆ 3.666667  ┆ 65.0      ┆ Stockout  │
│            ┆           ┆            ┆ e motor    ┆           ┆           ┆           ┆ risk      │
│ P006       ┆ 280       ┆ 150        ┆ Stainless  ┆ 7         ┆ 95.666667 ┆ 88.0      ┆ To add,   │
│            ┆           ┆            ┆ steel      ┆           ┆           ┆           ┆           │
│            ┆           ┆            ┆ flange     ┆           ┆           ┆           ┆           │
│ P004       ┆ 25        ┆ 40         ┆ Control    ┆ 14        ┆ 6.333333  ┆ 118.0     ┆ Stockout  │
│            ┆           ┆            ┆ board      ┆           ┆           ┆           ┆ risk      │
└────────────┴───────────┴────────────┴────────────┴───────────┴───────────┴───────────┴───────────┘
↳ 10 rows retrieved

② Stockout risk / To add, item
  P003 motor coil         Inventory:  380 Safety stock:  200 remnant:  41days LT:5days [To add,]
  P009 rolling bearing    Inventory:  450 Safety stock:  300 remnant:  43days LT:5days [To add,]
  P002 hydraulic cylinder Inventory:  120 Safety stock:  150 remnant:  52days LT:10days [Stockout risk]
  P010 electromagnetic valve Inventory:   65 Safety stock:   80 remnant:  57days LT:10days [Stockout risk]
  P007 three-phase motor  Inventory:    8 Safety stock:   15 remnant:  65days LT:14days [Stockout risk]
  P006 Stainless steel flange Inventory:  280 Safety stock:  150 remnant:  88days LT:7days [To add,]
  P004 Control board      Inventory:   25 Safety stock:   40 remnant: 118days LT:14days [Stockout risk]

Chart 1 for input 13

Interpreting the results

  • P002, P004, P007, and P010 are below their configured safety stock; lead times are 10–14 days.
  • Days-of-stock and safety-stock checks use different criteria. Review stock_status alongside open purchase orders, scheduled receipts, and demand changes.
  • For recurring monitoring, update the reference period and provide the flagged products for procurement review.

No.080: Extracting products with abnormally high or low sales

Practical use

Rapid increase and decline of monthly sales arises from various factors such as order concentration or loss, large orders from specific clients, and price fluctuations.
By automatically detecting this, Early Problem Identification and Seizing Opportunities becomes possible.

How the SQL works

Calculate the month-on-month change rate and extract months or products whose absolute value exceeds the threshold (e.g., ±30%):

Month-over-month change rate=Current month sales−Previous month salesPrevious month sales×100\text{Month-over-month change rate} = \frac{\text{Current month sales} - \text{Previous month sales}}{\text{Previous month sales}} \times 100

Using the window function LAG(), you can retrieve the previous month’s value for each product in chronological order:

LAG(monthly_revenue) OVER (PARTITION BY product_id ORDER BY ym)

PARTITION BY product_id allows you to calculate an independent month-over-month comparison for each product.

Run in Python

[IN: 14]
print("=== No.080 Extract products with abnormally high or low sales ===\n")

# (1) Monthly Sales by Product + Month-on-Month
print("① Compared to the previous month ±30%Abnormal records above")
df80 = q(conn, '''
WITH RECURSIVE months(month_start) AS (
  SELECT '2024-01-01'
  UNION ALL
  SELECT date(month_start, '+1 month')
  FROM months
  WHERE month_start < '2024-12-01'
),
monthly AS (
  -- Include zero-order months so LAG always refers to the preceding calendar month.
  SELECT
    p.product_id,
    p.product_name,
    strftime('%Y-%m', m.month_start) AS ym,
    COALESCE(SUM(o.quantity * o.unit_price), 0) AS monthly_revenue
  FROM products p
  CROSS JOIN months m
  LEFT JOIN orders o
    ON o.product_id = p.product_id
   AND strftime('%Y-%m', o.order_date) = strftime('%Y-%m', m.month_start)
  GROUP BY p.product_id, p.product_name, ym
),
with_lag AS (
  SELECT
    *,
    LAG(monthly_revenue) OVER (PARTITION BY product_id ORDER BY ym) AS prev_revenue
  FROM monthly
)
SELECT
  product_id,
  product_name,
  ym,
  monthly_revenue,
  prev_revenue,
  ROUND((monthly_revenue - prev_revenue) * 100.0 / prev_revenue, 1) AS mom_pct
FROM with_lag
WHERE prev_revenue > 0
  AND ABS((monthly_revenue - prev_revenue) * 100.0 / prev_revenue) >= 30
ORDER BY ABS(ROUND((monthly_revenue - prev_revenue) * 100.0 / prev_revenue, 1)) DESC
LIMIT 15
''')

# (2) Monthly Trend Data by Product (Total 12 Months)
df80_all = q(conn, '''
WITH RECURSIVE months(month_start) AS (
  SELECT '2024-01-01'
  UNION ALL
  SELECT date(month_start, '+1 month')
  FROM months
  WHERE month_start < '2024-12-01'
),
monthly AS (
  -- Include zero-order months so LAG always refers to the preceding calendar month.
  SELECT
    p.product_id,
    p.product_name,
    strftime('%Y-%m', m.month_start) AS ym,
    COALESCE(SUM(o.quantity * o.unit_price), 0) AS monthly_revenue
  FROM products p
  CROSS JOIN months m
  LEFT JOIN orders o
    ON o.product_id = p.product_id
   AND strftime('%Y-%m', o.order_date) = strftime('%Y-%m', m.month_start)
  GROUP BY p.product_id, p.product_name, ym
)
SELECT * FROM monthly
ORDER BY product_id, ym
''')

# Visualization: Monthly trends of the top 3 products (line lines)
TOP_PRODS = (df80['product_id'].unique(maintain_order=True).to_list())[:3]
COLORS_80 = ['#3498db', '#e74c3c', '#2ecc71']
ALL_YMS   = sorted(df80_all['ym'].unique().to_list())

fig, ax = plt.subplots(figsize=(11, 5))
for i, pid in enumerate(TOP_PRODS):
    sub  = df80_all.filter(pl.col('product_id') == pid).sort('ym')
    pname = sub['product_name'][0]
    revs  = [sub.filter(pl.col('ym') == ym)['monthly_revenue'][0] / 1_000_000
             if ym in sub['ym'].to_list() else 0 for ym in ALL_YMS]
    ax.plot(ALL_YMS, revs, marker='o', color=COLORS_80[i], linewidth=2,
            label=f'{pid} {pname}')
    # Highlighting anomalies
    anoms = df80.filter(pl.col('product_id') == pid)
    for row in anoms.iter_rows(named=True):
        ym_idx = ALL_YMS.index(row['ym']) if row['ym'] in ALL_YMS else -1
        if ym_idx >= 0:
            ax.scatter(row['ym'], row['monthly_revenue'] / 1_000_000,
                       color=COLORS_80[i], s=150, zorder=5,
                       marker='*', edgecolors='black', linewidth=0.5)

ax.set_title('By product Monthly Sales Trends (★ = Compared to the previous month ±30%The above abnormalities)No.080)', fontsize=12)
ax.set_xlabel('Year and month')
ax.set_ylabel('Monthly Sales (million yen)')
ax.legend(fontsize=9)
ax.grid(alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
[OUT: 14]
=== No.080 Extract products with abnormally high or low sales ===

① Compared to the previous month ±30%Abnormal records above
── SQL ─────────────────────────────────────────
  WITH RECURSIVE months(month_start) AS (
    SELECT '2024-01-01'
    UNION ALL
    SELECT date(month_start, '+1 month')
    FROM months
    WHERE month_start < '2024-12-01'
  ),
  monthly AS (
    -- Include zero-order months so LAG always refers to the preceding calendar month.
    SELECT
      p.product_id,
      p.product_name,
      strftime('%Y-%m', m.month_start) AS ym,
      COALESCE(SUM(o.quantity * o.unit_price), 0) AS monthly_revenue
    FROM products p
    CROSS JOIN months m
    LEFT JOIN orders o
      ON o.product_id = p.product_id
     AND strftime('%Y-%m', o.order_date) = strftime('%Y-%m', m.month_start)
    GROUP BY p.product_id, p.product_name, ym
  ),
  with_lag AS (
    SELECT
      *,
      LAG(monthly_revenue) OVER (PARTITION BY product_id ORDER BY ym) AS prev_revenue
    FROM monthly
  )
  SELECT
    product_id,
    product_name,
    ym,
    monthly_revenue,
    prev_revenue,
    ROUND((monthly_revenue - prev_revenue) * 100.0 / prev_revenue, 1) AS mom_pct
  FROM with_lag
  WHERE prev_revenue > 0
    AND ABS((monthly_revenue - prev_revenue) * 100.0 / prev_revenue) >= 30
  ORDER BY ABS(ROUND((monthly_revenue - prev_revenue) * 100.0 / prev_revenue, 1)) DESC
  LIMIT 15
───────────────────────────────────────────────
shape: (15, 6)
┌────────────┬───────────────────────────┬─────────┬─────────────────┬──────────────┬─────────┐
│ product_id ┆ product_name              ┆ ym      ┆ monthly_revenue ┆ prev_revenue ┆ mom_pct │
│ ---        ┆ ---                       ┆ ---     ┆ ---             ┆ ---          ┆ ---     │
│ str        ┆ str                       ┆ str     ┆ i64             ┆ i64          ┆ f64     │
╞════════════╪═══════════════════════════╪═════════╪═════════════════╪══════════════╪═════════╡
│ P002       ┆ hydraulic cylinder        ┆ 2024-03 ┆ 937775          ┆ 157498       ┆ 495.4   │
│ P004       ┆ Control board             ┆ 2024-12 ┆ 323916          ┆ 58770        ┆ 451.2   │
│ P003       ┆ motor coil                ┆ 2024-09 ┆ 1048418         ┆ 291108       ┆ 260.1   │
│ P010       ┆ electromagnetic valve     ┆ 2024-12 ┆ 583986          ┆ 166830       ┆ 250.0   │
│ P005       ┆ Industrial Rubber Packing ┆ 2024-12 ┆ 2115179         ┆ 662013       ┆ 219.5   │
│ …          ┆ …                         ┆ …       ┆ …               ┆ …            ┆ …       │
│ P010       ┆ electromagnetic valve     ┆ 2024-08 ┆ 574275          ┆ 242540       ┆ 136.8   │
│ P001       ┆ Precision Bearings        ┆ 2024-10 ┆ 2231815         ┆ 967514       ┆ 130.7   │
│ P007       ┆ three-phase motor         ┆ 2024-05 ┆ 313266          ┆ 137800       ┆ 127.3   │
│ P008       ┆ heat-resistant gasket     ┆ 2024-12 ┆ 2590185         ┆ 1170195      ┆ 121.3   │
│ P002       ┆ hydraulic cylinder        ┆ 2024-11 ┆ 1598703         ┆ 727570       ┆ 119.7   │
└────────────┴───────────────────────────┴─────────┴─────────────────┴──────────────┴─────────┘
↳ 15 rows retrieved
── SQL ─────────────────────────────────────────
  WITH RECURSIVE months(month_start) AS (
    SELECT '2024-01-01'
    UNION ALL
    SELECT date(month_start, '+1 month')
    FROM months
    WHERE month_start < '2024-12-01'
  ),
  monthly AS (
    -- Include zero-order months so LAG always refers to the preceding calendar month.
    SELECT
      p.product_id,
      p.product_name,
      strftime('%Y-%m', m.month_start) AS ym,
      COALESCE(SUM(o.quantity * o.unit_price), 0) AS monthly_revenue
    FROM products p
    CROSS JOIN months m
    LEFT JOIN orders o
      ON o.product_id = p.product_id
     AND strftime('%Y-%m', o.order_date) = strftime('%Y-%m', m.month_start)
    GROUP BY p.product_id, p.product_name, ym
  )
  SELECT * FROM monthly
  ORDER BY product_id, ym
───────────────────────────────────────────────
shape: (120, 4)
┌────────────┬───────────────────────┬─────────┬─────────────────┐
│ product_id ┆ product_name          ┆ ym      ┆ monthly_revenue │
│ ---        ┆ ---                   ┆ ---     ┆ ---             │
│ str        ┆ str                   ┆ str     ┆ i64             │
╞════════════╪═══════════════════════╪═════════╪═════════════════╡
│ P001       ┆ Precision Bearings    ┆ 2024-01 ┆ 1118340         │
│ P001       ┆ Precision Bearings    ┆ 2024-02 ┆ 1473495         │
│ P001       ┆ Precision Bearings    ┆ 2024-03 ┆ 3148803         │
│ P001       ┆ Precision Bearings    ┆ 2024-04 ┆ 1516340         │
│ P001       ┆ Precision Bearings    ┆ 2024-05 ┆ 1589777         │
│ …          ┆ …                     ┆ …       ┆ …               │
│ P010       ┆ electromagnetic valve ┆ 2024-08 ┆ 574275          │
│ P010       ┆ electromagnetic valve ┆ 2024-09 ┆ 470538          │
│ P010       ┆ electromagnetic valve ┆ 2024-10 ┆ 482986          │
│ P010       ┆ electromagnetic valve ┆ 2024-11 ┆ 166830          │
│ P010       ┆ electromagnetic valve ┆ 2024-12 ┆ 583986          │
└────────────┴───────────────────────┴─────────┴─────────────────┘
↳ 120 rows retrieved

Chart 1 for input 14

Interpreting the results

  • ★ mark (outliers) indicate months with a month-on-month increase of ±30% or more.
  • The sharp increase in months is likely due to factors such as “concentrated large orders,” “campaign effects,” and “switching away from competitors.”
  • A sharp decline in months may be a sign of ‘order suspension from key clients,’ ‘price negotiations underway,’ or ‘order suppression due to excess inventory.’
  • The threshold (±30%) can be adjusted according to industry and product life cycle and standardized as Internal anomaly detection rules

Applying these queries at work

Manufacturing B2B “Order Management SQL Pattern Collection”

ChallengesSQL PatternsBusiness Value
Automatic KPI AggregationGROUP BY + strftimeReducing the time required for monthly report creation
Customer RankingsORDER BY + TOP-NPriority Allocation of Sales Resources
Customer ClassificationCASE + DATE ComparisonVisualizing New Acquisition vs. Deepening Existing Ones
Repeat RateConditional aggregation + divisionCustomer loyalty KPI development
Detecting dormant customersMAX(date) + date differenceProactive measures to prevent defections
RFM SegmentCTE + CASE ScoringStrategy design by customer segment
Inventory managementLEFT JOIN + Inventory Months CalculationQuantitative Assessment of Excess Inventory / Stockout Risks
anomaly detectionLAG window functionAutomatic alerts for sales anomalies

”What can be automated with SQL” and “What human judgment is required”

SQL automates Data aggregation, transformation, and filtering,
The Cause Analysis of “why this change happened” requires human knowledge and experience.

  • SQL → Presenting “What, when, and how much changed”
  • Humans → Judge “Why Things Have Changed and How to Respond”

By clarifying this division of roles, the effectiveness of utilizing SQL is maximized.

Steps for practical use

1. Ensuring Data Quality

  • Regularly check orders for missing values, duplicates, and input errors (No.094–095).
  • Standardize the format of order_date (YYYY-MM-DD)

2. Automate Regular Execution

Every morning at 8:00 → Run inventory risk SQL → Email notifications for items at risk of out-of-stock
On the 1st of every month, → RFM recalculation → notify the person in charge of segment changes.
Execute the quarter-end → KPI dashboard SQL → automatically insert into management reports

3. Connecting Data Sources

In this exercise, we used SQLite, but in practice, we connect to the following systems:

SystemDB ExampleSupported SQL
Sales Management SystemMySQL / PostgreSQLAlmost identical (the date function has dialects)
ERP(SAP etc)SQL Server / OracleDialect support required
DWHBigQuery / SnowflakeRuns with almost standard SQL

4. Security and Permission Management

  • Order data and customer data may contain personal information
  • Set role-based access control (RBAC) for DWHs and assign Minimum necessary permissions

Conclusion

In this chapter, we practiced 10 Practical Data Analysis SQL using manufacturing B2B order and inventory data.

No.Acquired SkillsPractical Value
071Monthly KPI AggregationAutomatic Generation of Order Summaries
072Unit Price CalculationUnderstanding transaction characteristics by client
073Number of purchases by customerIdentifying VIP and Low-Frequency Customers
074New / Existing Customer ClassificationVisualizing the impact of new development
075Repeat Rate CalculationCustomer Loyalty KPIs
076Last Purchase Date & Number of Days PassedAutomatic detection of dormant customers
077RFM Analysis DataCustomer Segment Design
078Inventory vs. Demand ComparisonQuantitative Tracking of Inventory Overage and Shortage
079Stockout Risk ExtractionAutomation of replenishment order alerts
080Abnormal sales detectionEarly Detection of Rapid Increases and Decreases

Chapter 2 (Part 1)9Chapter) covers Applied Analysis SQL (cohort analysis, funnel analysis, A/B test aggregation, etc.).
Let’s master more advanced analytical methods by combining the basic aggregation patterns learned in this chapter.

Consulting for businesses

Building an order data analysis platform, Customer Analysis &RFM Implementation of Analytics,
Inventory risk management SQL automation, regarding SQL Training and in-house production support,
Suri Kobo accepts consultations for corporate clients.

As discussed in this exercise, we have a system for automating order KPIs, customer segments, and inventory alerts using SQL.
From implementation support to designing educational programs, please feel free to contact us.


📩 Contact Us: www.surikobo.co.jp/contact Please feel free to consult us first.