Okay, this is related to database given on http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
I need to find list of Customers,OrderID, ProductID for orders from Spain.
The table 'Orders' has OrderID and table 'Products' consists of ProductID whereas the table 'OrderDetails' consists of OrderID and ProductID. I use the following code but I get an error message 'Error: 1 ambiguous column: OrderID'
Here is my code
SELECT CustomerName, Country, OrderID, ProductID
FROM Customers, Orders, Products, OrderDetails
WHERE Customers.CustomerID = Orders.CustomerID
AND Orders.OrderID = OrderDetails.OrderID
AND Products.ProductID = OrderDetails.ProductID AND Country = 'Spain'
Can someone correct any mistake?
You need to use the alias names like this:
SELECT Customers.CustomerName,
Customers.Country, --Specify the correct table in which you have Country column
Orders.OrderID,
Products.ProductID
FROM Customers
inner join Orders ON Customers.CustomerID = Orders.CustomerID
inner join OrderDetails on Orders.OrderID = OrderDetails.OrderID
inner join Products on Products.ProductID = OrderDetails.ProductID
where Customers.Country = 'Spain'
Also try to avoid comma seperated JOINS. A good read: Bad habits to kick : using old-style JOINs
Related
I have a table named orders (orderID, customerID, purchaseDate, and paymentType) and orderLine (orderID, upc, productName, quantity, price). I'm trying to join these two tables so that it primarily shows the orders table but then has a total price column for each order on the far right. This is what I have so far but it's adding the total for every single row into one. I want each individual row to have their own sum by multiplying quantity*price based on the orderID.
SELECT orders.orderID, SUM(orderLine.price * orderLine.quantity)
FROM orderLine
LEFT JOIN orders
ON orders.orderID = orderLine.orderID
You need to add group by clause
SELECT orders.orderID, SUM(orderLine.price * orderLine.quantity)
FROM orderLine
LEFT JOIN orders
ON orders.orderID = orderLine.orderID
grou by orders.orderID
SELECT orders.orderID, (SUM(orderLine.price * orderLine.quantity) Over (Order
By orders.orderID))
FROM orderLine
LEFT JOIN orders
ON orders.orderID = orderLine.orderID
Group By orders.orderID
You want all orders, so start with orders. Table aliases also make the query easier to write and to read:
SELECT o.orderID, SUM(ol.price * ol.quantity)
FROM orders o LEFT JOIN
orderLine ol
ON o.orderID = ol.orderID
GROUP BY o.orderID;
Then the answer to your question is GROUP BY.
You would have a very poor data model if you have orderlines that don't have a corresponding order, which is what your version of the query suggests.
I have two tables: Customers and Orders. I do a left join of Customers and Orders on CustomerID column which is primary key in Customers and foreign key in Orders.
When I list CustomerID after joining, I get the list as expected.
When I count the number of CustomerID, again I get the number of record I am expecting.
When I use distinct count for CustomerID, i get an error.
1.
select Customers.CustomerID as list
from Customers left join Orders on Customers.CustomerID = Orders.CustomerID
where Customers.CustomerID = 4;
2.
select count(Customers.CustomerID) as numRecord
from Customers left join Orders on Customers.CustomerID = Orders.CustomerID
where Customers.CustomerID = 4;
3.
select count(distinct (Customers.CustomerID)) as numRecord
from Customers left join Orders on Customers.CustomerID = Orders.CustomerID
where Customers.CustomerID = 4;
I cannot understand where is the error. Any help would be appreciated.
The error:
Error in SQL:
Syntax error (missing operator) in query expression 'count(distinct Customers.CustomerID)'.
You don't need the () around Customers.CustomerID in count(distinct ..)
select count(distinct Customers.CustomerID) as numRecord
from Customers
left join Orders on Customers.CustomerID = Orders.CustomerID
where Customers.CustomerID = 4;
How do i use DISTINCT command in a SQL query to show the supplier id, company name, and the number of distinct products from that supplier that were ordered prior to a specific date? I ran the code in Access but it doesn't translate over to SQL efficiently. The table appears.
[Supplier ID Company Name Product Name Order Date
1 Exotic Liquids Chang 17-Aug-94
1 Exotic Liquids Chang 22-Nov-94
1 Exotic Liquids Aniseed Syrup 26-Sep-94]
The code I have so far is the following. Where I get confused is where to put the DISTINCT statement. Should it be immediately after the Select? Should it go in Parentheses in addition to the SELECT? Excuse my lack of knowledge on this subject in advance.
SELECT Suppliers.SupplierID, Customers.CompanyName, Products.ProductName,
Orders.OrderDate
FROM Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate <='1/1/1999'
ORDER BY Suppliers.SupplierID
You can either distinct by all columns selected :
SELECT DISTINCT
Suppliers.SupplierID, Customers.CompanyName, Products.ProductName,
Orders.OrderDate
FROM
Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE
Orders.OrderDate <='1/1/1999'
ORDER BY
Suppliers.SupplierID
or use group by instead if you need to distinct only by SupplierID. DISTINCT is not a function, hence DISTINCT(Suppliers.SupplierID) means the same as simply put DISTINCT word after SELECT in this case (see the 2nd reference below).
For Reference :
http://blog.sqlauthority.com/2007/12/20/sql-server-distinct-keyword-usage-and-common-discussion/
http://weblogs.sqlteam.com/jeffs/archive/2007/10/12/sql-distinct-group-by.aspx
I'm pretty sure it's this:
SELECT DISTINCT(Suppliers.SupplierID), Customers.CompanyName, Products.ProductName,Orders.OrderDate
FROM Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate <='1/1/1999'
ORDER BY Suppliers.SupplierID
I'm trying to find the revenue from customers in an country (United States).
The relevant tables are:
Order Details: Order ID, Unit price, quantity,
Orders: Order ID, customerID
Customers: customerID, country.
I'm not sure how to do this. I was thinking multiple inner join but it doesn't work.
The error message is "Syntax error (missing operator) in query expession 'ORDER DETAILS].ORDERID = ORDER.ORDERID
INNER JOIN CUSTOMERS ON CUSTOMERS.CUSTOMERID = ORDERS.CUSTOMERID'
MS online said Error 3075
Here is what I have:
SELECT SUM(QUANTITY*UNITPRICE) AS Result
FROM [ORDER DETAILS]
INNER JOIN ORDERS ON [ORDER DETAILS].ORDERID = ORDER.ORDERID
INNER JOIN CUSTOMERS ON CUSTOMERS.CUSTOMERID = ORDERS.CUSTOMERID
WHERE COUNTRY = 'Argentina'
Thanks in advance.
Edit: table structure
http://postimg.org/image/oojygytkv/
In Access if you are JOINing more than two tables together then it requires parenthesis. Try the following:
SELECT SUM(QUANTITY * UNITPRICE) AS Result
FROM ([ORDER DETAILS]
INNER JOIN ORDERS ON [ORDER DETAILS].ORDERID = ORDERS.ORDERID)
INNER JOIN CUSTOMERS ON CUSTOMERS.CUSTOMERID = ORDERS.CUSTOMERID
WHERE COUNTRY = 'Argentina'
This is the code I did
SELECT TOP 5 ContactName FROM Customers
INNER JOIN [Order Details]ON OrderId =
CustomerID
INNER JOIN Orders ON ProductID = OrderID
WHERE UnitPrice >= 25000
ORDER BY ContactName ASC
But this is the error I am getting
Msg 209, Level 16, State 1, Line 5
Ambiguous column name 'orderID'
Can someone explain to me why I am getting this error.
This is what I am trying to do is show the most recent five orders that were purchased from a customer who has spent more than $25,000
So i am assuming to use order,product,and customer.
The column OrderID exists in both tables.
There is probably an OrderID column in both your Order Details and your Orders table, and SQL Server doesn't know which one to take.
Solution: specify which one you want to use by putting the table name in front of it:
Orders.OrderID instead of just OrderID
So your query would look like this then:
SELECT TOP 5 ContactName FROM Customers
INNER JOIN [Order Details]ON Orders.OrderId =
CustomerID
INNER JOIN Orders ON ProductID = Orders.OrderID
WHERE UnitPrice >= 25000
ORDER BY ContactName ASC
Almost certainly you have the field orderID in both the Details and the Orders table.
Clarify it with either Orders.orderID or Details.orderID.
There are 2 OrderID columns across the tables.
You can remove the ambiguity with aliases (like this) or use Orders.OrderID
SELECT TOP 5 C.ContactName
FROM
Customers C
INNER JOIN
[Order Details] OD ON C.OrderId = OD.CustomerID
INNER JOIN
Orders O ON OD.ProductID = O.OrderID
WHERE O.UnitPrice >= 25000 -- or OD?
ORDER BY C.ContactName ASC
Note: did you mean to joion Customers and [Order Details] like using Customers.OrderId?
When you JOIN multiple tables in the same query, you need to differentiate any columns which have the same name in multiple tables. Otherwise, how would the query engine know which one you're talking about?
You can do this either by prefixing the column name with <table name>. or <table alias>..
For example:
SELECT TOP 5
C.ContactName
FROM
Customers C -- Customers is aliased as "C"
INNER JOIN [Order Details] OD ON
OD.OrderId = C.CustomerID
INNER JOIN Orders O ON
OD.ProductID = O.OrderID
WHERE
OD.UnitPrice >= 25000
ORDER BY
C.ContactName ASC
Another important question... are you sure that you're joining on the correct columns there? It looks really wrong.
Finally, if this is for a homework assignment, please make sure that you tag it as such with the "homework" tag.
In your references to OrderId, you need to figure out which table you are pulling the orderId column from. (In some cases, you can just pick either table). Let's call that table .
In your query, replace orderId with .orderId.