What I'm trying to do is multiply two columns from separate tables. Below is the query I tried, but I'm getting a syntax error near "*".
SELECT quantity
FROM includes
INNER JOIN product
ON includes.quantity = product.productprice;
SELECT includes quantity * productprice
AS totalprice
FROM product
Quantity is one of the columns inside of the table "include" and productprice is one of the columns inside the table "product." I used INNER JOIN to try to join these tables together and tried to multiply the two columns together under a new variable totalprice. What am I doing wrong here?
I've corrected the syntax of your query but the columns in the join are surely not the ones noted.
Please replace column_name with the names of the matching columns.
SELECT quantity * productprice AS totalPrice
FROM includes
INNER JOIN product
ON includes.column_name = product.column_name;
Related
I have two tables I need information from, Products and Suppliers. Both these tables have a SupplierID column I am trying to use to join them together to retrieve the right info.
The output I need is SupplierID and ContactName from the Suppliers table. The correct output should contain only two suppliers, so I attempted something like this, but ran into a conversion error converting nvarchar value to a data type int. I am not supposed to count how many products they supply but aggregate functions seem like the best method to me.
SELECT TOP 2 ContactName, COUNT(Products.SupplierID) AS Supply
FROM Products
LEFT JOIN Suppliers ON Suppliers.ContactName = Products.SupplierID
GROUP BY Products.SupplierID, Suppliers.ContactName
ORDER BY Supply;
I have tried many different queries but none will work. I am confused on how to join these tables without running into errors. All the products have a unique ProductID as well. The correct output should look something like this:
7 John Smith
12 John Sample
Both these tables have a SupplierID column I am trying to use to join them together to retrieve the right info
If so, you should be joining on that column accross tables.
Also, it is a good practice to use table aliases and prefix each column with the table it belongs to.
Another remark is that if you want suppliers that sell the most different products, then you want to order by descending count (not ascending).
Finally, if you want to left join, then you should start from the suppliers and then bring in the products, not the other way around.
Consider:
select top 2
s.SupplierID,
s.ContactName,
COUNT(*) as Supply
from Suppliers s
left join Products p on p.SupplierID = s.SupplierID
group by s.SupplierID, s.ContactName
order by Supply desc;
You're currently joining on two different fields:
on Suppliers.ContactName = Products.SupplierID
Presumably this should be as follows?
on Suppliers.SupplierID = Products.SupplierID
i have two tables, one with ''product''(id_product,id_company,product_name,description,price) and a table ''order'' (id_order,id_client,id_product,product_quantity) and I would like to multiply product.price with order.product_quantity and put out the result in a new column in the ''order'' table, preferably a computed column
thanks in advance
join them using id_product
SELECT p.price * o.product_quantity newcolumn
FROM product p
JOIN order o
ON p.id_product = o.id_product
I have following tables: Product and ProductEvent. Some of the relevant columns of Product table are SocialID, MarketID, LocalID. These three columns have numbers that corresponds to one column called EventID in ProductEvent.
Here is my question: is there a better way to join these tables where I do not have to make 3 separate joins all pointing to match EventID?
Maybe something like this:
select *
from Product P join ProductEvent PE
on (PE.EventID = P.SocialID or PE.EventID = P.MarketID or PE.EventID = P.LocalID)
I am trying to create a query based on trying to find a value in one column of a table based on the values of another column in another table. This is the code I have written so far,
SELECT OrderDetails.OrderDetailID FROM OrderDetails
INNER JOIN OrderDetails
ON Products.ProductID = OrderDetails.ProductID
WHERE Products.SupplierID="5";
By executing the code, I want to find the OrderDetailID of the row in the OrderID Table where the SupplierID in the Products table is 5. For example,
Highlighted Products Table
The SupplierID of "Queso Cabrales" is 5 and its ProductID is 11. This corresponds to the foreign key in the table OrderDetail of 11 and therefore I want the primary key of that row to be returned. In this case 1.
Highlighted OrderDetails Table
At the moment I get an "ambiguous column name" error in the first line even though I have declared both the table and the column name. Also, how am I meant to ask SQL to fetch the data regarding other tables. I know I am mean't to use "INNER JOIN" but how do I execute so the WHERE command can be used.
You have OrderDetails twice in the FROM clause but no Products. I think you mean:
SELECT od.OrderDetailID
FROM OrderDetails od INNER JOIN
Products p
ON p.ProductID = od.ProductID
WHERE p.SupplierID = 5;
Notes:
Table aliases make the query easier to write and read (the od and p).
Don't use delimiters around numeric constants. I assume that SupplierId is a number, so I removed the double quotes.
You are using two time the same column so you must resove the amvbiguity eg: this way (using two distinct alias od1, od2)
SELECT od1.OrderDetailID FROM OrderDetails od1
INNER JOIN OrderDetails as od2
ON Products.ProductID = od2.ProductID
WHERE Products.SupplierID="5";
One problem is that INNER JOIN was designed to generate duplicate columns in the result. This problem was solved in 1992 when the SQL standard introduced 'relational' join types that don't generate duplicate attributes:
SELECT OrderDetailID
FROM OrderDetails
NATURAL JOIN Products
WHERE SupplierID = 5;
I've wanted to create a view in my sql but I've been getting this error:
Duplicate column name 'product_id'
Here's my query:
CREATE VIEW product_view AS
SELECT
*
FROM
products
JOIN storeitems on products.product_id = storeitems.product_id;
I believe creating an alias would solve the problem but then I don't know how to do it. I hope you could help me with this. Thanks!
You get the error because both tables in your query has a column named product_name and selected column names must be unique.
You make your aliases like this:
CREATE VIEW product_view AS SELECT p.product_name, si.product_name StoreItemsProductName -- continue with what you want to select...
FROM products p JOIN storeitems si
on p.product_id=si.product_id;
yes, creating an column alias would solve the problem and you do it like
select column1 as test_column
But if this your original query then I doubt you won't get this error since you are selecting from single table called products and obviously same table won't have multiple column with same name.
Anyway you can modify your query to be like
CREATE VIEW product_view AS
SELECT p.product_id as product_product_id,
si.product_id as storeItem_product_id
FROM
products p
JOIN storeitems si on p.product_id = si.product_id;
Probably both the products table and the storeitems table have a column named product_name.
CREATE VIEW product_view AS SELECT * FROM products p JOIN storeitems si on p.product_id=si.product_id;
here p will be the alias for the products table and si will be the alias for storeitems table
If your database supports the using clause and you only have one duplicate column name, then you could do:
select *
from products p join
storeitems si
using (product_id);
In general, though, I think it is better to list out the columns in a view. If one of the underlying tables changes the order or names of the columns, then this is safer.