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 - sql

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.

Related

How to create a new column in query then populate with a simple calc X*Y

I have two columns. One is MW and the other is Price. I want to create a sql query that creates a third imaginary column called AwardPrice, and within that it will be one column (MW) * another column (Price). These two columns already exist in the same fact table.
I would recommend a computed column:
alter table t add AwardPrice as (MW * Price);
This value is calculated when you query the table, so the value is always accurate and up-to-date.
How to create a new column in query . . .
I want to create a sql query that creates...
I suppose that you want to do that in a SELECT statement as
SELECT *, MW * Price As AwardPrice
FROM YourTableNameHere;

SQL-Creating Table

I am stuck with this question. I know table can be created with the select statement.
CREATE TABLE new_orders (ordr_id,order_date DEFAULT SYSDATE,cus_id)
AS
Select order_id.order_date,customer_id FROM Orders;
Which statement would be true regarding the above question ?
The NEW_ORDERS table would not get created beacuse the default value cannot be specified in the column definition.
The NEW_ORDERS table would get created and only the NOT NULL constraint define on the specified columnns would be passed to the new table.
The NEW_ORDERS table would not get created because the column names in the CREATE TABLE command and the SELECT clause do not match.
The NEW_ORDERS table would get created and all the constraints defined on the specified columns in the orders table would be passed to new Table.
I think correct answer should be 3, but it's wrong. The correct answer according to my book is 2. I don't get it how?
The answer should not be 3. Because in the select statement, you are taking values from Orders table. So you will use the column names of that table. Selecting value from there, you are inserting it into new_orders. So, the column names don't have to be same because they are different tables.

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

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.

Can I create Dynamic Column Names in DB2?

I two tables, one table contains the names of columns, the other table contains the actual data that I need to display in a report. The data and columns are linked by a column key.
Is there a way to build a select statement that can use the column name from the column table for the actual temp table column?
Something like this;
Select A.value AS B.ColumnName
Simply put Can I create a table column name based on the value in another table in sql?
You canĀ“t do it without using dynamic SQL as mentioned question that can be useful for you.
SQL Server: How to set column alias from another table during select?

Sql Column had no values

I have a sql table that I am trying to add a column from another table to. Only when I execute the alter table query it does not pull the values out of the table to match the column where I am trying to make the connection.
For example I have column A from table 1 and column A from table 2, they are supposed to coincide. ColumnATable1 being an identification number and ColumnATable2 being the description.
I tried this but got an error...
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200)
where v_venkey = v_vendorno
It tells me that I have incorrect syntax... Anyone know how to accomplish this?
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200);
go
update c
set c.V_VendorName = a.V_VendorName
from CommittedTbl c
join TableA a
on c.v_venkey = a.v_vendorno;
go
I'm just guessing at your structure here.
alter table 2 add column A <some_type>;
update table2 set column A = (select column_A from table2 where v_venkey = v_vendorno);
Your names for tables and columns are a bit confusing but I think that should do it.
There is no WHERE clause for an ALTER TABLE statement. You will need to add the column (your first two lines), and then insert rows based upon a relationship you define between the two tables.
ALTER TABLE syntax:
http://msdn.microsoft.com/en-us/library/ms190273%28v=sql.90%29.aspx
There are several languages within SQL:
DDL: Data Definition Language - this defines the schema (the structure of tables, columns, data types) - adding a column to a table affects the table definitions and all rows will have that new column (not just some rows according to a criteria)
DML: Data Manipulation Language - this affects data within a table, and inserting, updating or other changes fall into this and you can update some data according to criteria (and this is where a WHERE clause would come in)
ALTER is a DDL statement, while INSERT and UPDATE are DML statements.
The two cannot really be mixed as you are doing.
You should ALTER your table to add the column, then INSERT or UPDATE the column to include appropriate data.
Is it possible that you want a JOIN query instead? If you want to join two tables or parts of two tables you should use JOIN.
have a look at this for a start if you need to know more LINK
hope that helps!