Quick answer
ClickHouse is an open source columnar database built for fast analytical queries over large tables. Instead of reading whole rows, it reads only the columns a query touches, so a sum over millions of rows returns in seconds. This post loads ten messy shop orders into a local ClickHouse server, totals paid sales by city, and fixes the type error that stops the first sum() attempt.
A Karachi shop keeps its orders in a spreadsheet, and the monthly question is simple: how much did each city pay? With a few hundred rows the spreadsheet answers it. With a few hundred thousand it crawls, and every new question means another pivot table. The shop owner does not need a data warehouse team or a cloud bill, just a database that scans fast and asks for little in return. ClickHouse is built for exactly that shape of problem: wide tables, lots of rows, and queries that aggregate rather than update single records. The rest of this post sets it up on a laptop and runs the totals on real order data.
What I would check before using it
My first check is the shape of the question. ClickHouse earns its place when queries scan many rows and return few: sums, counts, and averages by city, day, or product. If the work is looking up one order by id, updating a status, or handling a checkout, a row store fits better, and the PostgreSQL post covers that side of the choice. Picking ClickHouse for single-row lookups is like hiring a delivery van to fetch one envelope.
My second check is how the data arrives. ClickHouse is happiest with data that lands in batches: a nightly export, an hourly file, a stream of events. It stores rows in sorted parts and merges them in the background, so constant small updates cost more than they are worth. If every order must be visible the second it is placed, a transactional database should sit in front, with ClickHouse fed afterwards.
My third check is what the team already knows. ClickHouse speaks SQL, so anyone comfortable with SELECT, WHERE, and GROUP BY starts immediately. The SQL Joins post covers the query basics. The new parts are the table engines and the type system, and both are strict in ways that surprise people used to looser databases.
My verdict, as judgement: adopt ClickHouse when the same analytical questions run over growing piles of mostly read-only data. The recommendation flips for a small shop whose spreadsheet still answers in seconds, or for an app that needs fast single-row writes.
Loading shop orders into ClickHouse and totalling paid sales by city
The demo is a small Karachi shop with ten orders in a CSV file. Some totals say "Rs 1,250", some are plain numbers like 980.50, and one row is a cancelled order that must not count. Everything runs on a laptop against a local server.
The server here runs from the standalone binary, downloaded from the ClickHouse site; the Docker image clickhouse/clickhouse-server runs the same server where Docker is available. Check the version first:
$ ./clickhouse --version
ClickHouse local version 26.12.1.1069 (official build).
Create a database and a table. The table uses the MergeTree engine, the standard choice for analytics, and orders rows by order date so date-range queries skip most of it:
CREATE DATABASE shop;
CREATE TABLE shop.orders (
order_id String,
customer String,
city LowCardinality(String),
total_rs String,
status LowCardinality(String),
order_date Date
) ENGINE = MergeTree()
ORDER BY order_date;
Note that total_rs is a String on purpose. The raw export mixes currency symbols with plain numbers, so the text loads as it is and gets cleaned in the query. The orders file, orders.csv, holds ten rows:
order_id,customer,city,total_rs,status,order_date
ORD-2001,Ayesha Khan,Karachi,"Rs 1,250",paid,2026-11-20
ORD-2002,Bilal Ahmed,Lahore,980.50,paid,2026-11-21
ORD-2003,Sana Malik,Islamabad,"Rs 2,400",paid,2026-11-21
ORD-2004,Usman Raza,Karachi,1150,paid,2026-11-22
ORD-2005,Hira Shah,Lahore,"Rs 750",cancelled,2026-11-22
ORD-2006,Ahmed Noor,Karachi,2850,paid,2026-11-23
ORD-2007,Fatima Zahra,Islamabad,"Rs 1,875",paid,2026-11-23
ORD-2008,Zain Ali,Karachi,"Rs 2,100",paid,2026-11-24
ORD-2009,Maryam Iqbal,Islamabad,1450,paid,2026-11-25
ORD-2010,Daniyal Hussain,Lahore,"Rs 3,300",paid,2026-11-25
Load it and count the rows:
$ ./clickhouse client --query "INSERT INTO shop.orders FORMAT CSVWithNames" < orders.csv
$ ./clickhouse client --query "SELECT count() AS rows FROM shop.orders"
10
Now the obvious query:
SELECT
city,
count() AS order_count,
sum(total_rs) AS total_pkr
FROM shop.orders
WHERE status = 'paid'
GROUP BY city
ORDER BY total_pkr DESC
It fails, with a precise error:
Received exception from server (version 26.12.1):
Code: 43. DB::Exception: Received from localhost:9000. DB::Exception:
Illegal type String of argument for aggregate function sum.
(ILLEGAL_TYPE_OF_ARGUMENT)
ClickHouse refuses to guess. sum() needs a number, and total_rs is a String, so the query stops instead of adding text silently. The fix cleans the value in the query: strip everything that is not a digit or a decimal point, then convert to a float:
SELECT
city,
count() AS order_count,
round(sum(toFloat64(replaceRegexpAll(total_rs, '[^0-9.]', ''))), 2) AS total_pkr
FROM shop.orders
WHERE status = 'paid'
GROUP BY city
ORDER BY total_pkr DESC
The result:
┏━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ city ┃ order_count ┃ total_pkr ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Karachi │ 4 │ 7350 │
│ Islamabad │ 3 │ 5725 │
│ Lahore │ 2 │ 4280.5 │
└───────────┴─────────────┴───────────┘
Karachi leads with four paid orders at Rs 7,350, Islamabad has three at Rs 5,725, and Lahore two at Rs 4,280.50. The cancelled Lahore order for Rs 750 stays out. For a bigger table the same query is still one scan over a single column.
Common mistakes and how to fix them
First, summing a text column directly. The symptom is Code 43, "Illegal type String of argument for aggregate function sum". The fix is to clean the value first, as above, or use a numeric column once the format is known.
Second, declaring the totals column as Float64 at load time. The symptom is the insert failing on the first "Rs 1,250" value, because the CSV parser cannot turn currency text into a number. The fix is to load the raw text as a String and convert it in a query, or clean the file before loading.
Third, forgetting the status filter. The symptom is totals that look right but run slightly high, because cancelled orders sneak into the sum. Here the Lahore total would have included the cancelled Rs 750. The fix is WHERE status = 'paid', checked against a count of rows by status.
Fourth, picking a MergeTree ORDER BY key that matches nothing the queries filter on. The symptom is queries that scan the whole table even for one day of data. The fix is to order by the column the queries filter and group by, usually a date, and keep the key to one or two columns.
Frequently Asked Questions
Do I need Docker to try ClickHouse? No. The standalone binary runs the server with one command, as this post does, and the Docker image is an alternative. Either way the data lives in a local directory.
Is ClickHouse a replacement for PostgreSQL? Not for everyday app data. PostgreSQL handles single-row reads and writes, transactions, and updates, while ClickHouse handles scans and aggregations over large tables. Many teams run both, PostgreSQL in front and ClickHouse fed from it.
How is ClickHouse different from DuckDB? Both run analytical SQL, but DuckDB is a single file for one process and ClickHouse is a server many clients can query at once. The DuckDB post covers the single-file case, and this post covers the step up to a server.
Can I clean messy data inside ClickHouse instead of before loading? Yes, up to a point. Functions like replaceRegexpAll and toFloat64 clean values in the query. For cleaning that runs on a schedule with tests, the dbt post covers a tested pipeline.
What to remember
- ClickHouse stores columns separately, so queries read only the columns they need.
- MergeTree is the standard engine; its ORDER BY key decides which data a query can skip.
- sum() needs numbers; text totals must be cleaned with replaceRegexpAll and toFloat64 first.
- Load messy exports as text and clean in the query, instead of failing the insert.
- A WHERE clause on status keeps cancelled orders out of the totals.
Where this leaves you
Ten messy orders went into a local ClickHouse server, and the paid totals by city came back in one query: Karachi Rs 7,350, Islamabad Rs 5,725, Lahore Rs 4,280.50. The type error was the useful part: ClickHouse refused to add text and said so plainly instead of guessing. That strictness is the whole deal: fast scans, honest errors, and a table engine that rewards a good sort key. Take an export of your own, load it as text, and run one GROUP BY query against it.
Further reading
- ClickHouse tutorial
- MergeTree table engine
- sum aggregate function
- DuckDB: Analytics Queries on a File, Without a Server
- PostgreSQL Explained: The Reliable Database You Default To
- dbt Tutorial: Clean Shop Orders Data With SQL Models
- SQL Joins Explained: INNER, LEFT, RIGHT, FULL Examples
- Polars Python Tutorial: Fast DataFrames Without Pandas
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
Originally published at https://practicaldevblog.blogspot.com/2026/11/clickhouse-shop-orders-analytics.html

Top comments (0)