Multiplying two columns of same table and storing result to the third column of same table - sql

I m having a table
SALES(sno,comp_name,quantity,costperunit,totalcost)
After supplying the costperunit values, totalcost need to be calculated as "totalcost=quantity*costperunit".
I want to multiply 'quantity' and 'costperunit' columns and store the result in 'totalcost' column of same table.
I have tried this:
insert into SALES(totalcost) select quantity*costperunit as res from SALES
But It failed!
Somebody please help me in achieving this..
Thanks in Advance

Try updating the table
UPDATE SALES SET totalcost=quantity*costperunit

You need to use update.
UPDATE SALES SET totalcost=quantity*costperunit

It would be better if you do not calculate this field manually but make it a computed column instead - so it calculates automatically for you.
You can change the column with following query:
ALTER TABLE Sales
DROP COLUMN totalcost
ALTER TABLE Sales
ADD totalcost AS quantity*costperunit
DEMO

try this while inserting new row
INSERT INTO test(sno,comp_name,quantity,costperunit,totalcost)
Values (1,#comp_name,#quantity,#costperunit,#quantity*#costperunit)

Best not to store fields that can be calculated, but if you want to, with SQL Server you can set a field to be calculated automatically when values are there.

Related

Store a calculated math equation in a Query?

I am working on this:
I would like to know how to create a new column that holds the amount equal to LocalCurrency multiplied by ExchangeRate for each rows of item.
Use a computed column:
alter table t add newcolumn as (localcurrency * exchangerate);
This adds the column to the table so it is calculated automatically when you retrieve it. The value is automatically set so no update is needed.
You can calculate when required in a query or can create a view to do so and use it wherever required.
CREATE VIEW Purchase_details AS
SELECT *, localcurrencyamount*exchangerate
FROM your_table_name
Select * from Purchase_details

How do aggregate function on two cells of a record in a database table and show its result in another table in MS SQL Server

I have two tables Product and SellingIncome as shown in the screenshot below, and I need in the SellingIncome table to set for the SellingIncome column to insert automatically the multiplication of columns ProductPrice and SellingCount in the Product table (I mean ==>
SellingIncome.SellingIncome = Product.ProductPrice * Product.SellingCount
How can solve this problem? Any help will be appreciated!
Forget about the "sellingincome" table. You can just use a computed column in product:
alter table product add column sellingincome as
(productprice * sellingcount);
Using a computed column means that sellingincome is always up-to-date.

Copy column with mathematic SQL Server

I'm new to SQL Server. I have column with value of product, now I want to add column value + tax (tax = 10% value) on the same table. Any suggestion how to do this the fastest way or should I insert 1 by 1? Can I just copy value column and add some mathematics ? If I can do that can someone show how it work? Thanks.
If the tax is always the same, you can use a computed column:
alter t add value_plus_tax as (value * 1.1);
This calculates the value whenever you need it. Otherwise, you should add the column explicitly and update it:
alter t add value_plus_tax decimal(10, 2); -- or whatever
update t
set value_plus_tax = value * 1.1;
If your tax rate can change, you can store the rate in a separate table and use a view that joins the two tables to retrieve the data...
CREATE TABLE TaxRate (
[Percent] TINYINT
)
GO
CREATE VIEW ValuesWithTax AS
SELECT *, [Value] * [Percent] / 100 ValueWithTax FROM [Values]
CROSS JOIN TaxRate
If tax rate is not a fix value, you can add a column to save tax rate and a computed column to save total value
ALTER TABLE table1 add tax_rate decimal(19,4) DEFAULT(0.1) WITH VALUES
ALTER TABLE table1 add toatal_value as ([value](1+tax_rate))
Add computed column like (having all calculation).
Ie : Below example i have considered sum of value and tax column you can do any calculation (Make sure the calculation should not throw exception consider boundary condition ) .
ALTER TABLE dbo.tableName ADD RetailValue AS (value+tax );
you can make it persisted computed column too.

How do you create a totals row with percentage in SQL

I have SQL that generates output like that shown in the attached image under "FIGURE A", but need to expand it to look like "FIGURE B".
Can anyone assist with the SQL? Thanks
don't know what Database you are using so try Oracle.
First you need to create a new table include the new column PERCENT_COMPLETED
select STATE,WIDGETS_REQUIRED,WIDGETS_COMPLETED,
WIDGETS_COMPLETED/WIDGETS_REQUIRED*100 | '%' as PERCENT_COMPLETED
from table;
then you need to insert a new row into it
select
'Total' as STATE,
sum(WIDGETS_REQUIRED) as WIDGETS_REQUIRED,
sum(WIDGETS_COMPLETED) as WIDGETS_COMPLETED,
sum(WIDGETS_COMPLETED)/sum(WIDGETS_REQUIRED)*100 | '%' as PERCENT_COMPLETED
into new_table
from table;
In MYSQL, I tried this solution.
First, insert a row with 3 values: STATE, WIDGETS_REQUIRED, WIDGETS_COMPLETED. You can insert these 3 values later, but it is more convenient insert them now.
insert into FIGURE_A values
("TOTAL", 48, 33);
Second, add a column: percent_complete
alter table FIGURE_A
ADD percent_complete decimal(5,2);
Here you should notice that you need to add a constraint. See this question.
So, we need to add the check constraint:
alter table FIGURE_A
ADD constraint chk_range CHECK (percent_complete>=0 and percent_complete<=100);
Finally, we just add the percentage values. See here. There are many ways to calculate the percentage. And this question is more suitable for your case, because you need to update, not just calculate.
update FIGURE_A
set percent_complete = WIDGETS_COMPLETED*100/WIDGETS_REQUIRED
The final result may look like this.

Inserting one table's complete column data to a particular column in another table in Microsoft SQL

I m having two tables COMPONENTS(sno,comp_name,quantity,costperunit,totalcost) and COST(sno,comp_name,costperunit).. in Microsfot SQL Server
I m prompting user to enter costperunit value and updating immediately in COST table.
I want to import all the values of costperunit column to COMPONENTS from COST Table.
I have tried this :
insert into COMPONENTS(costperunit)
select costperunit from COST where COST.sno=COMPONENTS.sno
but I m unable to achieve the required functionality.
Somebody please suggest me better query to do this..
Thanks in Advance..
If you want to update (!) all values, why are you trying to insert ? Try to use "update from select" way:
UPDATE components
SET components.costperunit=cost.costperunit
FROM cost
INNER JOIN components ON cost.sno=components.sno
Even better to add WHERE clause to update only changed values: