Performing Mathematical Calculations
Another frequent use for calculated fields is performing mathematical calculations on retrieved data. Let's take a look at an example. The Orders table contains all orders received, and the OrderItems table contains the individual items within each order. The following SQL statement retrieves all the items in order number 20008:
SELECT prod_id, quantity, item_price FROM OrderItems WHERE order_num = 20008;
prod_id quantity item_price ---------- ----------- --------------------- RGAN01 5 4.9900 BR03 5 11.9900 BNBG01 10 3.4900 BNBG02 10 3.4900 BNBG03 10 3.4900
The item_price column contains the per unit price for each item in an order. To expand the item price (item price multiplied by quantity ordered), ...
Get Sams Teach Yourself SQL in 10 Minutes, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.