want to multiply values from two columns of a table. Example:Our database has a table named purchase with data in the following columns: id, name, price, quantity, and discount_id.
Let’s multiply the price by the quantity of the products to find out how much you paid for each item in your order. Solution:SELECT name, price*quantity AS total_price FROM purchase;This query returns records with the name of the product and its total price:
Discussion:Do you need to select the name of each record (in our case, name) and compute for it the result of multiplying one numeric column by another (price and quantity)? All you need to do is use the multiplication operator (*) between the two multiplicand columns (price * quantity) in a simple SELECT query. You can give this result an alias with the AS keyword; in our example, we gave the multiplication column an alias of total_price. Note that you can also use data from two columns coming from different tables. We have another table in our database named discount that has columns named id and value; the latter represents the percent discount on the item with the given ID. Look at the example below. Solution:SELECT p.name, p.price*p.quantity*(100-d.value)/100 AS total_price FROM purchase p JOIN discount d ON d.id=p.discount_id;Here’s the result:
As you can see, it’s quite easy to multiply values from different joined tables. In our example above, we multiplied the price of each product by its quantity from one table (purchase) and then multiplied this total price by the percent discount using the discount table.
/en/access2013/designing-your-own-database/content/ Calculated fields and totals rows let you perform calculations with the data in your tables. Calculated fields perform calculations using data within one record, while totals rows perform a calculation on an entire field of data. Calculated fieldsWhen you create a calculated field, you are adding a new field in which every row contains a calculation involving other numerical fields in that row. To do this, you must enter a mathematical expression, which is made up of field names in your table and mathematical symbols. You don't need to know too much about math or expression building to create a useful calculated field. In fact, you can write robust expressions using only grade-school math. For instance, you could:
To find the total number of brownies that have been sold, we'll have to multiply the number of units sold by the numerical value of that unit—here, 2*12, which equals 24. This was a simple problem, but performing that calculation for each row of the table would be tedious and time consuming. Instead, we can create a calculated field that shows the product of these two fields multiplied together on every row. To create a calculated field:
For more examples of mathematical expressions that can be used to create calculated fields, review the Arithmetic Expressions in the Expression Builder dialog box. Arithmetic operators in the Expression BuilderTotals rowsThe totals row adds up an entire column of numbers, just like in a ledger or on a receipt. The resulting sum appears in a special row at the bottom of your table. For our example, we'll add a totals row to our calculated field. This will show us the total number of items sold. To create a totals row:
/en/access2013/creating-a-parameter-query/content/ |