Question

I have a huge table and I would like to loop through each row, do the following calculation and insert the new value in a new column.

For example:

Old_Table1:

Product      Quantity     Price
  TV            20         350€

New_Table:

Product      Quantity     Price   TOtal
  TV            20         350€    7000€

How to write a query to do the following.

If anyone familiar with implementing the same with HANA then it would be great.

Was it helpful?

Solution

Try below SQL:

CREATE TABLE new_table 
AS 
(
SELECT Product, Quantity, Price, (Quantity*Price) AS Total 
FROM old_table
)

SQL Fiddle

ANOTHER WAY:

ALTER TABLE old_table ADD COLUMN (Total Float);

UPDATE old_table SET Total = Quantity*Price;

SQL Fiddle 2

OTHER TIPS

One way is to use a cursor.

Here you can find more about it with example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top