SELECT * FROM Products_Joined, Products
WHERE p.ProductManufacturer = 'Sony'
ORDER BY p.ProductCode
I keep getting the error The multi part identifier p.ProductManufacturer could not be bound
I tried:
Setting the Order By
Adding the PRODUCTS table to the FROM
Is there something I'm missing?
You should use:
SELECT p.*, pj.*
FROM dbo.Products p
INNER JOIN dbo.ProductsJoined pj ON ..... <== add your missing JOIN condition here
WHERE p.ProductManufacturer = 'Sony'
ORDER BY p.ProductCode
First of all: never use SELECT * in your production code.
Secondly: use the proper ANSI JOIN syntax (INNER JOIN..) to clearly show what you're joining, and on what JOIN condition (which is missing in your case - you're producing a cartesian product here.....)
Third: if you use table aliases like p. - you need to define them, too!
You have no p object. You need to alias one of your tables.
SELECT * FROM Products_Joined, Products AS p
WHERE p.ProductManufacturer = 'Sony'
ORDER BY p.ProductCode
That will fix your immediate problem, however you should have a JOIN on your tables or else you are doing a CROSS JOIN, which is usually not preferable. An example of what it would look like is below.
SELECT *
FROM Products_Joined
JOIN Products AS p
ON Products_Joined.ProductsID = p.ProductsID
--This join is a guess on what the common column is between these two tables
--Change as necessary
WHERE p.ProductManufacturer = 'Sony'
ORDER BY p.ProductCode
UPDATE BASED ON YOUR COMMENT
If you received the error even with a Products.ProductManufacturer, then you are probably missing the ProductManufacturer column in the Products table. I would check your schema and verify the column exists.
Related
I am a user working in inventory management attempting to return information regarding product name, location, and its availability at the location from the adventure works 2017 database. While trying to run the query, I am getting an error message that states I have too many expressions in my subquery list and that I can only do that if I start the subquery with 'Exists'. I suppose I do not understand what I am doing wrong, maybe someone could explain how 'Exists' works? Is there a way I can rewrite this so I can return both expressions in the subquery? I will include the syntax and error message below.
SELECT Production.Product.Name
,(SELECT Production.Location.Name
,Production.Location.Availability
FROM Production.Location
WHERE Production.Location.LocationID = Production.ProductInventory.LocationID)
FROM Production.Product
INNER JOIN Production.ProductInventory
ON Production.Product.ProductID = Production.ProductInventory.ProductID;
You don't need a subquery here this appears to be just an outer join:
select p.Name, l.Name Location, l.Availability
from Production.Product p
join Production.ProductInventory i
left join Production.Location l on l.LocationID = i.LocationID
on p.ProductID = i.ProductID;
Note how tidier the query is by using table aliases.
I have a task in which I'm using Northwind database to select only these products which category name begins with 'C'. Product details are in table Products while CategoryName is in table Categories and this is the only answer I came up with
SELECT P.*
FROM Products P, Categories C
WHERE C.CategoryName LIKE 'C%' AND P.CategoryID = C.CategoryID
I was curious if I can do it without putting Categories inside FROM clause and connecting the tables. For me it seemed logical that I can access child table and use the values inside it smh, can you explain why it's not possible?
You can use a Correlated Subquery to achieve this:
SELECT *
FROM Products p
WHERE EXISTS (SELECT id FROM Categories c WHERE c.id = p.id)
This subquery is "Correlated" in that the SQL in the subquery references the query in which it's contained. Very similar to a join, but we are down in the WHERE clause using the EXISTS condition.
EXISTS is nice since it doesn't care what is returned by the subquery. It only cares that ANYTHING was returned by the subquery. So that subquery could also be: SELECT 1 FROM Categories WHERE c.id = p.id and this query would still work.
You should be using JOINS not that type of syntax (INNER, LEFT, RIGHT, ...)
But you can do this
SELECT P.*
FROM dbo.Products P WITH (NOLOCK)
INNER JOIN dbo.Categories C WITH (NOLOCK)
ON P.CategoryID = C.CategoryID
WHERE
SUBSTRING(LTRIM(C.CategoryName), 1, 1) = 'C'
Join Products with Categories using an INNER JOIN which will only return the records that match. Add WITH (NOLOCK) when selecting from a transactional database so it does not lock users when attempting to run a transaction. I try to avoid LIKE when possible so in your scenario you can use substring to get the first character. The first argument is the expression, then start index and then how many characters you need to grab. LTRIM will remove white spaces from the left so even if your CategoryName accidentally has a leading whitespace or spaces it will remove them before running the substring. RTRIM would be unnecessary for what you need
I have two tables, CustomerCost and Products that look like the following:
I am joining the two tables using the following SQL query:
SELECT custCost.ProductId,
custCost.CustomerCost
FROM CUSTOMERCOST Cost
LEFT JOIN PRODUCTS prod ON Cost.productId =prod.productId
WHERE prod.productId=4
AND (Cost.Customer_Id =2717
OR Cost.Customer_Id IS NULL)
The result of the join is:
joins result
What i want to do is when I pass customerId 2717 it should return only specific customer cost i.e. 258.93, and when customerId does not match then only it should take cost as 312.50
What am I doing wrong here?
You can get your expected output as follows:
SELECT Cost.ProductId,
Cost.CustomerCost
FROM CUSTOMERCOST Cost
INNER JOIN PRODUCTS prod ON Cost.productId = prod.productId
WHERE prod.productId=4
AND Cost.Customer_Id = 2717
However, if you want to allow customer ID to be passed as NULL, you will have to change the last line to AND Cost.Customer_Id IS NULL. To do so dynamically, you'll need to use variables and generate the query based on the input.
The problem in the original query that you have posted is that you have used an alias called custCost which is not present in the query.
EDIT: Actually, you don't even need a join. The CUSTOMERCOST table seems to have both Customer and Product IDs.
You can simply:
SELECT
Cost.ProductId, Cost.CustomerCost
FROM
CUSTOMERCOST Cost
WHERE
Cost.Customer_Id = 2717
AND Cost.productId = 4
You seem to want:
SELECT c.*
FROM CUSTOMERCOST c
WHERE c.productId = 4 AND c.Customer_Id = 2717
UNION ALL
SELECT c.*
FROM CUSTOMERCOST c
WHERE c.productId = 4 AND c.Customer_Id IS NULL AND
NOT EXISTS (SELECT 1 FROM CUSTOMERCOST c2 WHERE c2.productId = 4 AND c2.Customer_Id = 2717);
That is, take the matching cost, if it exists for the customer. Otherwise, take the default cost.
SELECT custCost.ProductId,
custCost.CustomerCost
FROM CUSTOMERCOST Cost
LEFT JOIN PRODUCTS prod
ON Cost.productId =prod.productId
AND (Cost.Customer_Id =2717 OR Cost.Customer_Id IS NULL)
WHERE prod.productId=4
WHERE applies to the joined row. ON controls the join condition.
Outer joins are why FROM and ON were added to SQL-92. The old SQL-89
syntax had no support for them, and different vendors added different,
incompatible syntax to support them.
Can anyone who knows SQL, specifically the flavor used in Microsoft Access 2013, tell me what I'm doing wrong here?
SELECT custid, custname, ordno, itemno, itemname
FROM cust
INNER JOIN order
ON cust.custid = order.custid
INNER JOIN orderitems
ON order.ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
I've already read other, similar questions, and tried the methods they used in their solutions, but I'm getting a "Syntax error in FROM clause.", almost no matter what I try.
* * *
SOLUTION: Thanks for the replies! In addition to adding square brackets around "order" and using TableName.ColumnName syntax in SELECT, I had to use parentheses for my multiple INNER JOINs. Here is the fixed code:
SELECT cust.custid, cust.custname, [order].ordno, orderitems.itemno, inv.itemname
FROM ((cust
INNER JOIN [order]
ON cust.custid = [order].custid)
INNER JOIN orderitems
ON [order].ordno = orderitems.ordno)
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
SELECT cust.custid --<-- Use two part name here
,cust.custname
,[order].ordno
,orderitems.itemno --<-- Only guessing here use the correct table name
,inv.itemname --<-- Only guessing here use the correct table name
FROM cust
INNER JOIN [order]
ON cust.custid = [order].custid --<-- used square brackets [] around ORDER as it is
INNER JOIN orderitems -- a key word.
ON [order].ordno = orderitems.ordno
INNER JOIN inv
ON orderitems.itemno = inv.itemno;
In your Select Statament you need to use Two Part name i.e TableName.ColumnName since these column can exist in more than one Tables in your FROM clause you need to tell sql server that columns in your select coming from which table in your from clause.
This is working fine
SELECT i.*,o.*,p.*
FROM orders o
INNER JOIN oitems i
ON i.orderid = o.orderid
LEFT OUTER JOIN products p
ON i.catalogid = p.catalogid
however i want to perform a nested select as an example this is giving the coulmn x exists more than once
SELECT AA.*,
FROM (
SELECT i.*,o.*,p.*
FROM orders o
INNER JOIN oitems i
ON i.orderid = o.orderid
LEFT OUTER JOIN products p
ON i.catalogid = p.catalogid ) AA
i know the second example makes no sense , but i need another select with groupping, is there a way to fix the coulm exists more than once error without having to specify the column names in the select statement?
By using *, you are getting the same column more than once in your output. To avoid, this, specifically state the columns you want returned, instead.
The culprits are likely orderid and catalogid which both exist in more than one table, but there may be others.
You have mention column names specifically. I can see that you are using "*". you have to use like select i.columnName,o.columnName etc