A worked example
By the end of this page you will have built one view from an orders table, an order lines table, a stores table and a customers table, and run five queries that show the view is ready to connect.
The example follows the rest of this section. The names are made up, and the SQL is plain SQL with one note for the warehouses that differ.
The tables
Section titled “The tables”| Table | One row is | Notes |
|---|---|---|
orders |
One version of an order | A new row is added each time an order changes. version rises, order_total is the order’s amount, ordered_on is text. |
order_lines |
One line of an order | line_amount and quantity are the line’s own figures. |
stores |
One store | store_no is a number, region and format are text, floor_area is a store figure. |
customers |
One customer | segment is text. |
The view
Section titled “The view”The view has one row for each order line, keeps only the latest version of each order, and leaves out the order’s total and the store’s floor area, because adding either one up would repeat it on each line.
CREATE VIEW reporting.sales ASWITH latest_orders AS ( SELECT order_id, store_no, customer_id, ordered_on, status FROM ( SELECT o.*, ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY version DESC) AS rn FROM orders o ) ranked WHERE rn = 1)SELECT CAST(lo.ordered_on AS DATE) AS order_date, -- a real date, first in the view ol.order_id AS order_id, -- counted: its name holds order and ends in id ol.order_line_id AS order_line_id, CAST(lo.store_no AS VARCHAR(10)) AS store_code, -- a code, stored as text s.region AS region, s.format AS store_format, c.segment AS customer_segment, lo.status AS order_status, ol.quantity AS quantity, -- a number, added up ol.line_amount AS revenue_usd -- a number, added up, currency in the nameFROM order_lines olJOIN latest_orders lo ON lo.order_id = ol.order_idLEFT JOIN stores s ON s.store_no = lo.store_noLEFT JOIN customers c ON c.customer_id = lo.customer_id;QUALIFY ROW_NUMBER() ... = 1 does the same in one step on BigQuery, Snowflake and Databricks. The form above also
works on PostgreSQL, Redshift and the Microsoft warehouses. In T-SQL, write VARCHAR(10) as it is, and in BigQuery
write STRING.
Where a value is missing, such as a store that has no row in stores, the left joins leave region null, and the view
does not fill it with a word such as “Unknown” or “N/A”. The fifth check below looks for such stand-ins.
Check the view
Section titled “Check the view”Run these five queries against the view. Each one has an answer you can read at a glance.
-
The row is one line: the two numbers match.
SELECT COUNT(*) AS row_count, COUNT(DISTINCT order_line_id) AS distinct_lines FROM reporting.sales; -
No order repeats a version: every order id has one status.
SELECT order_id FROM reporting.sales GROUP BY order_id HAVING COUNT(DISTINCT order_status) > 1;The query returns no rows.
-
The totals match the source for a month you trust.
SELECT SUM(revenue_usd) FROM reporting.salesWHERE order_date >= DATE '2026-10-01' AND order_date < DATE '2026-11-01';The number equals the month’s revenue in the report your finance team already signs off.
-
The date range is real: no date is far from the others.
SELECT MIN(order_date) AS first_day, MAX(order_date) AS last_day FROM reporting.sales;A first day of 1900-01-01 means a stand-in is hiding in the date.
-
The nulls are nulls: no column holds a stand-in.
SELECT COUNT(*) FILTER (WHERE region IS NULL) AS no_region,COUNT(*) FILTER (WHERE region IN ('', 'N/A', 'Unknown')) AS stand_insFROM reporting.sales;stand_insis 0. On a warehouse withoutFILTER, useSUM(CASE WHEN ... THEN 1 ELSE 0 END).
When the five pass, connect the warehouse and pick the view. The strip over the Dashboard then has little to ask, and the figure that you checked in the third query is the figure on the card.