DWH Interview Question 21 - What are the types of fact tables?

Data Warehouse Interview Q&A · Question 21

In this post we are going to cover the three types of fact tables: transaction, periodic snapshot and accumulating snapshot. This is a classic interview question, and the interviewer usually wants more than the names. They want to know what one row means in each type and which business question each one answers.

A fact table always has a grain, the thing one row stands for. The three types differ in that grain. A transaction fact stores one row per event. A periodic snapshot stores the state of something at the end of every period. An accumulating snapshot stores one row per process, like an order, and fills it in as the process moves forward.

We will build one small table of each type for TB Shop, query them, and see how they behave differently.

Question 21 of the Real DWH Question and Answers series on TechBrothersIT.

The interview answer

Say this first

A transaction fact has one row per event, inserted once (a sale line). A periodic snapshot has one row per entity per period, showing its state (stock per product per day). An accumulating snapshot has one row per process instance (an order), updated at each milestone, with a date per milestone and lag measures.

If you remember one detail, remember this: only the accumulating snapshot updates its rows.

Let's create some sample data

We need three tables: fact_sales for sales events, fact_inventory_daily for the stock level of each product at the end of each day, and fact_order_fulfillment with one row per order.

Sample data — run this first
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS fact_inventory_daily;
DROP TABLE IF EXISTS fact_order_fulfillment;

-- 1) TRANSACTION fact: one row per event, inserted once, never updated
CREATE TABLE fact_sales (
  sale_date DATE,
  product   VARCHAR(50),
  quantity  INT,
  amount    DECIMAL(10,2)
);
INSERT INTO fact_sales VALUES
  ('2026-01-05', 'Desk Lamp',      1, 30.00),
  ('2026-01-05', 'Wireless Mouse', 2, 50.00),
  ('2026-01-07', 'Desk Lamp',      1, 30.00);

-- 2) PERIODIC SNAPSHOT fact: one row per product per day, even if nothing happened
CREATE TABLE fact_inventory_daily (
  snapshot_date DATE,
  product       VARCHAR(50),
  on_hand       INT
);
INSERT INTO fact_inventory_daily VALUES
  ('2026-01-05', 'Desk Lamp', 39), ('2026-01-05', 'Wireless Mouse', 58),
  ('2026-01-06', 'Desk Lamp', 39), ('2026-01-06', 'Wireless Mouse', 58),
  ('2026-01-07', 'Desk Lamp', 38), ('2026-01-07', 'Wireless Mouse', 58);

-- 3) ACCUMULATING SNAPSHOT fact: one row per order, UPDATED at each milestone
CREATE TABLE fact_order_fulfillment (
  order_id        INT PRIMARY KEY,
  order_date      DATE,
  ship_date       DATE,
  delivery_date   DATE,
  days_to_ship    INT,
  days_to_deliver INT
);
INSERT INTO fact_order_fulfillment VALUES
  (1001, '2026-01-05', '2026-01-07', '2026-01-10', 2, 3),
  (1002, '2026-01-06', '2026-01-07', NULL,         1, NULL),
  (1003, '2026-01-08', NULL,         NULL,         NULL, NULL);

Let's look at the sales first.

Query
SELECT * FROM fact_sales ORDER BY 1;
Result3 rows · 4 columns
sale_dateproductquantityamount
2026-01-05Desk Lamp130.00
2026-01-05Wireless Mouse250.00
2026-01-07Desk Lamp130.00

Three sales: two on January 5 and one on January 7. Each row is one event, inserted once.

Query
SELECT * FROM fact_inventory_daily ORDER BY 1;
Result6 rows · 3 columns
snapshot_dateproducton_hand
2026-01-05Desk Lamp39
2026-01-05Wireless Mouse58
2026-01-06Desk Lamp39
2026-01-06Wireless Mouse58
2026-01-07Desk Lamp38
2026-01-07Wireless Mouse58

The inventory table has one row per product per day, six rows for two products over three days. Notice that January 6 is there too.

Query
SELECT * FROM fact_order_fulfillment ORDER BY 1;
Result3 rows · 6 columns
order_idorder_dateship_datedelivery_datedays_to_shipdays_to_deliver
10012026-01-052026-01-072026-01-1023
10022026-01-062026-01-07NULL1NULL
10032026-01-08NULLNULLNULLNULL

The fulfillment table has one row per order, with a date for each milestone. Order 1001 has been delivered, order 1002 has shipped but not arrived, and order 1003 was just placed, so its ship and delivery dates are still NULL.

Step 1: Transaction fact - sales per day

Now let's try the most common type. We want total sales per day from fact_sales.

Query
SELECT sale_date, SUM(amount) AS sales
FROM fact_sales
GROUP BY sale_date
ORDER BY sale_date;
Result2 rows · 2 columns
sale_datesales
2026-01-0580.00
2026-01-0730.00

We get 80.00 on January 5 and 30.00 on January 7. January 6 is not in the result at all, because nothing was sold that day. Transaction facts are sparse: no event, no row. They are also the most detailed type, and they answer questions like what did we sell, to whom and when.

Step 2: Periodic snapshot - stock level every day

Now let's look at the snapshot. TB Shop records the units on hand for each product at the end of every day. Let's read the Desk Lamp rows.

Query
SELECT snapshot_date, on_hand
FROM fact_inventory_daily
WHERE product = 'Desk Lamp'
ORDER BY snapshot_date;
Result3 rows · 2 columns
snapshot_dateon_hand
2026-01-0539
2026-01-0639
2026-01-0738

There is a row for every day, including January 6, when the level stayed at 39. On January 7 it drops to 38, which matches the one Desk Lamp sold that day. Snapshots are dense: the state is recorded whether or not anything happened.

Tip

On hand is a semi-additive measure. You can add it across products, but never across days. Adding 39 + 39 + 38 does not give you any real stock level.

Step 3: Accumulating snapshot - order 1003 ships

This is the type that surprises people. When order 1003 ships, the pipeline does not insert a new row. It goes back to the existing row for that order and fills in the ship date and the number of days it took.

Statement
UPDATE fact_order_fulfillment
SET ship_date = '2026-01-09',
    days_to_ship = 1      -- calculated by the pipeline
WHERE order_id = 1003;

Now let's check the result.

Query
SELECT * FROM fact_order_fulfillment ORDER BY order_id;
Result3 rows · 6 columns
order_idorder_dateship_datedelivery_datedays_to_shipdays_to_deliver
10012026-01-052026-01-072026-01-1023
10022026-01-062026-01-07NULL1NULL
10032026-01-082026-01-09NULL1NULL

Order 1003 now has a ship date of 2026-01-09 and days_to_ship of 1. The delivery date is still NULL, and it will be filled in by another update when the parcel arrives. Still three rows, one per order.

Step 4: Accumulating snapshot - average days to ship

Because every milestone for an order sits on one row, lag measures are easy. Let's ask how long orders take to ship.

Query
SELECT ROUND(AVG(days_to_ship), 2) AS avg_days_to_ship
FROM fact_order_fulfillment
WHERE ship_date IS NOT NULL;
Result1 row · 1 column
avg_days_to_ship
1.33

The three shipped orders took 2, 1 and 1 days, so the average is 1.33 days. There is no need to match a ship event to an order event, the lag is already a column on the row.

The three types side by side

TransactionPeriodic snapshotAccumulating snapshot
GRAINOne row per eventEntity per periodOne row per process
GROWS WITHActivityTime (dense)Number of orders
UPDATESInsert onlyInsert onlyRows updated
MEASURESAmount, quantityLevels, balancesLags between milestones
  • Grain - transaction is one row per event, periodic snapshot is one row per entity per period, accumulating snapshot is one row per process instance.
  • Growth - transaction tables grow with activity, snapshots grow steadily with time, accumulating tables grow with the number of orders.
  • Updates - only the accumulating snapshot updates existing rows. The other two are insert only.

Which one answers which question?

In an interview you will often get a business question and be asked which fact table you would build.

  • "What did we sell yesterday?" - a transaction fact.
  • "What was our stock level at month end?" - a periodic snapshot.
  • "How long do orders take to ship?" - an accumulating snapshot.

Follow-up questions interviewers ask

Which fact type is updated?

Only the accumulating snapshot; transaction and periodic snapshot facts are insert-only.

Can one business process have several fact types?

Yes, for example order transactions plus an order fulfillment accumulating snapshot.

Why is a periodic snapshot dense?

It records the state for every entity every period, even when nothing happened.

The full script

Here is everything from this post in one script. Copy it and run it top to bottom.

Full script — copy and run top to bottom
-- ========================================================================
-- Data Warehouse Interview Q&A #21 - Transaction, snapshot and accumulating facts
-- TechBrothersIT. Sample data is invented for teaching.
-- Standard SQL, tested on PostgreSQL 16. Runs top to bottom in one go.
-- Run it in an empty database or schema: other videos reuse table names
-- like dim_customer, and their leftover tables can block the DROPs below.
-- PostgreSQL example:  CREATE SCHEMA dwh_q21;  SET search_path TO dwh_q21;
-- ========================================================================

-- Step 0: create and load the sample data
DROP TABLE IF EXISTS fact_sales;
DROP TABLE IF EXISTS fact_inventory_daily;
DROP TABLE IF EXISTS fact_order_fulfillment;

-- 1) TRANSACTION fact: one row per event, inserted once, never updated
CREATE TABLE fact_sales (
  sale_date DATE,
  product   VARCHAR(50),
  quantity  INT,
  amount    DECIMAL(10,2)
);
INSERT INTO fact_sales VALUES
  ('2026-01-05', 'Desk Lamp',      1, 30.00),
  ('2026-01-05', 'Wireless Mouse', 2, 50.00),
  ('2026-01-07', 'Desk Lamp',      1, 30.00);

-- 2) PERIODIC SNAPSHOT fact: one row per product per day, even if nothing happened
CREATE TABLE fact_inventory_daily (
  snapshot_date DATE,
  product       VARCHAR(50),
  on_hand       INT
);
INSERT INTO fact_inventory_daily VALUES
  ('2026-01-05', 'Desk Lamp', 39), ('2026-01-05', 'Wireless Mouse', 58),
  ('2026-01-06', 'Desk Lamp', 39), ('2026-01-06', 'Wireless Mouse', 58),
  ('2026-01-07', 'Desk Lamp', 38), ('2026-01-07', 'Wireless Mouse', 58);

-- 3) ACCUMULATING SNAPSHOT fact: one row per order, UPDATED at each milestone
CREATE TABLE fact_order_fulfillment (
  order_id        INT PRIMARY KEY,
  order_date      DATE,
  ship_date       DATE,
  delivery_date   DATE,
  days_to_ship    INT,
  days_to_deliver INT
);
INSERT INTO fact_order_fulfillment VALUES
  (1001, '2026-01-05', '2026-01-07', '2026-01-10', 2, 3),
  (1002, '2026-01-06', '2026-01-07', NULL,         1, NULL),
  (1003, '2026-01-08', NULL,         NULL,         NULL, NULL);


-- Step 1: Transaction fact: sales per day (days with no sales have no rows)
SELECT sale_date, SUM(amount) AS sales
FROM fact_sales
GROUP BY sale_date
ORDER BY sale_date;

-- Step 2: Periodic snapshot: Desk Lamp stock level every day
SELECT snapshot_date, on_hand
FROM fact_inventory_daily
WHERE product = 'Desk Lamp'
ORDER BY snapshot_date;

-- Step 3: Accumulating snapshot: order 1003 ships, so its row is UPDATED
UPDATE fact_order_fulfillment
SET ship_date = '2026-01-09',
    days_to_ship = 1      -- calculated by the pipeline
WHERE order_id = 1003;

-- check the result
SELECT * FROM fact_order_fulfillment ORDER BY order_id;

-- Step 4: Accumulating snapshot: average days to ship
SELECT ROUND(AVG(days_to_ship), 2) AS avg_days_to_ship
FROM fact_order_fulfillment
WHERE ship_date IS NOT NULL;

Key points

  • Transaction: one row per event, insert only, sparse
  • Periodic snapshot: one row per entity per period, dense, semi-additive
  • Accumulating snapshot: one row per process, updated at milestones
  • Accumulating facts make lags (days to ship) easy
  • Pick the type from the business question
Next in this series

Question 22 — What is a factless fact table? What is a factless fact table?

Every query and every result on this page was run on PostgreSQL 16 using the script above. The SQL is standard and runs on most databases with small changes (noted in the script). All tables and data are invented demo data for teaching.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.