Modelling and data changes
OLTP, OLAP and star schemas
Separate transaction systems from analytical models.
Understand the concept
OLTP supports many small consistent operations, such as inserting an order. OLAP aggregates larger datasets for analysis. The two workloads may need different models and indexes.
In a star schema, a fact table stores measures at a declared grain and dimension tables add product, customer or calendar context. If FactSales has order-line grain, COUNT(*) is not an order count. Slowly changing dimensions may be needed to preserve history: decide whether reports need the current customer city or the city at sale time.
Use it when building a warehouse or dashboard model from a transactional system.
Building all measures with COUNT/SUM without defining fact grain.
One topic. Three levels of depth.
SELECT OrderId, ProductId, Quantity, UnitPrice FROM lab.OrderItem;A source of line-grain measures.
This page does not execute SQL. Practise in the downloadable SqlStudioLab using SSMS. Before rerunning temporary-table examples, remove only the relevant #table. Views, procedures and functions create persistent lab objects; never use a production connection.
Try it yourself first.
Calculate units sold by category for paid orders.
Show the solution and explanation
SELECT p.Category, SUM(i.Quantity) AS Units FROM lab.OrderItem AS i JOIN lab.Product AS p ON p.ProductId = i.ProductId JOIN lab.SalesOrder AS o ON o.OrderId = i.OrderId WHERE o.Status = 'Paid' GROUP BY p.Category;Units sold use SUM(Quantity); COUNT(*) measures lines, not units.