I want to make a calculated columns from different tabel, i want to calculate TotalPrice from table A, which is from Quantity on Table B, and UnitPrice on Table C.
sorry for my bad english
Like #marc_s explain you, you can create computed only with column of the same table. If you want a "table" with a calcul between multiple table you can create simply a view.
Create view on SQL SERVER
Related
I am working in PrestoDB to query and create tables to configure data in a way that I can draw upon it and work on it in Excel and PowerBI. I am trying to create a persisted calculated column that is simply the quotient of two other existing columns.
A colleague suggested
Create Table B as
Select * , Column A/Column B as Column Q
from Table A
however when I perform
Select *
from Table B
column Q is there but completely empty.
What can I run to permanently add these computed columns so that when I query this data the values are persisted?
I don't think PrestoDB supports computed columns, much less persisted computed columns.
I think what you want is view, though, not a table:
create view v_a
select a.*, ColumnA/ColumnB as ColumnQ
from A a;
Anyone who queries v_b will see ColumnQ with the latest calculation of the ratio.
I'm trying to create the calculated column 'TotalWeight' (table SP) based on 'Quantity' column (table SP) and 'Weight' column (table P).
I can't figure it out, how to write expression. The one I tried doesn't work.
[SP.Quantity] * [P.Weight]
You have two options. First you could convert the table to a view in the database and do the join there. However if you prefer to do this in the DSV some syntax like the following will work. Not knowing your exact table names I guessed and hope you can extrapolate to your table names. Let’s call p the Product table. And let’s call sp the SupplierProduct table which is where you are putting the calculated column.)
Change the calculated column definition to:
Quantity * (select P.Weight from Product p where p.ProductID = SupplierProduct.ProductID)
I am working on a project in ACCESS 2010 that requires me to give a rank to 30000 products based on their sales. I have tried using a query to do the ranking and it takes a long time. (Please find codes at Making the ranking query efficient)
I figured out that instead, I can just sort the table based on the Sales column and add a field with numbers 1 to 30000.
So is there a way to add such a column, i.e. a column without any relationship to the existing table.
Add a field to the actual table? If that's the case, make a table and run this query:
ALTER TABLE yourTableName
ADD COLUMN yourColumnName AUTOINCREMENT(1, 1)
I have a rather interesting task ahead of me and I want to make sure I am thinking it out correct -
I have a table Part that has its part_id being used as part of the candidate keys of several other tables (A, B, C). I need to drop Part and use Product instead. A, B, C need to have their part_id column (which is of type bigint) replaced with a new column of product_id (which is of datatype int). I need to use the part_id column of the tables to determine what product_id I need to use for each row.
Here is what I am thinking is what I need to do (thoughts?):
create the product_id column in each of the tables (A, B, C)
set the product_id of each row for each table to the appropriate value
drop any constraints/fk/pk I have for the part_id column in A, B, C
drop the part_id from those tables completely
recreate the constraints/fk/pk I dropped earlier, only have product_id be part of them instead
drop the Part table completely
Can anyone see any potential issues that I may be neglecting?
Thanks!
Additional Info: Tables A, B, and C each have LOTS of data, so if there is more performant way, I am all ears.
You should be able to create new constraints on product_id before you drop constraints on part_id, and before you drop the column part_id. And you should do it in that way--create the new constraints first.
If things go wrong with the product_id changes, your database is still usable if the part_id constraints are still in place.
Can you please forward me the answer for my oracle project?
I have two different tables, invoice and customer. In the invoice table I already have a column called date and in the customer table I don't have the date column and I already created. I don't know how to bring the date data from the invoice table. Can anyone answer this question for me?
I think using keywords like "date" as column or table names is asking for trouble. It should be "creation_date" or "invoice_date" - more descriptive and not a keyword.
If that's correct, then the "date" in the customer table should be a "birth_date" or "signup_date" or something else meaningful for a customer, but NOT just a copy of "invoice_date". The DRY principle and normalization rules say you should not repeat data.
It isn't entirely clear what you want, but adding and populating a column on a table is easy enough:
1) Adding a new column:
alter table customer add (some_date date);
(as duffymo has said, you can't have a column called "date" in an Oracle table - or at least, you shouldn't).
2) Populating a column from data in another table:
update customer c
set some_date = (select max(other_date) from invoices i
where i.customer_id = c.customer_id
);
I used max() because I assume a customer may have more than one invoice. Of course, your needs may be different, but the important thing is that the subquery must only return 1 row for each customer somehow.