user3247608
user3247608

Reputation: 593

How to display a calculated field alongside the original table in sql?

I have a table called Stock, it contains a variety of fields of which two of them are price and quantity. How do I display all of the contents of the original table as well as the calculated field(quantity * price).

I tried

SELECT (Quantity * Price) FROM Stock; 

This displayed the calculated field, but I am having trouble displaying this calculated field alongside the original table.

Upvotes: 0

Views: 38

Answers (2)

padarom
padarom

Reputation: 3648

This is pretty easy to accomplish. If you want every value you can just use the *-wildcard in your SELECT statement:

SELECT *, (Quantity * Price) AS Calculation FROM Stock;

This will result in something like this:

ID  |  Name   |  Quantity  |  Price  |  Calculation
----+---------+------------+---------+---------------
 0  | Banana  | 100        | 10      | 1000
 1  | Apple   | 15         | 10      | 150
 2  | Orange  | 20         | 5       | 100
 3  | Pizza   | 3          | 15      | 45

As you can see, using the AS-keyword, you have created an alias for the calculation and the result will be accessible by this alias.

Upvotes: 1

marcosh
marcosh

Reputation: 9008

use

SELECT *, Quantity * Price FROM Stock;

This will print first all the fields of the Stock table and then your calculated field

Upvotes: 1

Related Questions