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

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;

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.

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.

MS SQL Computed column

I want to create a column based on COUNT(*) on another table, and when a record is deleted from that table it should decrease the value in this new column and vice versa. So, here is the query:
SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID
And it returns this:
And when I try to create a computated column like this:
CREATE TABLE test(
NumberOF as (SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID) )
I get the following error:
Subqueries are not allowed in this context. Only scalar expressions are allowed.
Here is the main table that I want to compute from:
How can I resolve this ?
You can define a UDF:
create function dbo.NumberOfFakturaID(#id int) returns int as begin
return (select count(1) from Korisnik1_FakturaStavka where id=#id)
end
and then use it as the computed column:
CREATE TABLE test(FakturaID int, NumberOF as dbo.NumberOfFakturaID(FakturaID))
But putting that sort of calc as a computed column should be used with care.
This is too long for a comment.
You can do this by defining a function to calculate the count and using that function in the computed column definition. However, I don't think this is a good idea for frequently used columns, because you will be doing a lot of counting "behind the scenes".
Alternatives:
Set up a view or materialized view with the additional count column.
Do the count explicitly when you need it.
Set up a trigger to store the count in the first table, whenever rows are inserted/updated/deleted from the second table.

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?