I hope that you can help me with this slightly thorny problem. I am using the W3Schools SQL tutorial and in the process of doing this, I am inventing 'real-world' queries to try and get some experience at this stuff. Using their database, I am trying to find out who ordered what using the following:
SELECT c.CustomerName, p.ProductName
FROM Customers c inner join Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od on od.OrderID = o.OrderID
JOIN Products p on p.ProductID = od.ProductID;
This keeps returning the error:
Syntax error (missing operator) in query expression 'c.customerid = o.customerid join orderdetails od on od.orderid = o.orderid join products p on p.productid = od.productid'.
After lots of fiddling about, and having a more experienced colleague look at my query, we can't find what's wrong with what I have written.
Please could you provide me with some help/guidance.
this query is not wrong and not giving any error i'm ruining this query on w3school http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
open this link and paste the query.
SELECT c.CustomerName, p.ProductName
FROM Customers c inner join Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od on od.OrderID = o.OrderID
JOIN Products p on p.ProductID = od.ProductID;
Refer to their documentation here - https://www.w3schools.com/sql/sql_join_inner.asp
Section - "JOIN Three Tables"
If I re-write your code like below then it works fine in W3School's try it editor.
SELECT c.CustomerName, p.ProductName
FROM ( ( ( Customers c inner join Orders o on c.CustomerID = o.CustomerID )
inner JOIN OrderDetails od on od.OrderID = o.OrderID )
inner JOIN Products p on p.ProductID = od.ProductID );
Related
I wrote a program about finding products sold in both France and Germany. On the other hand, I'm not happy with my subqueries. I have a feeling that all the joins could be skipped and done much faster, yet I could not find an answer to this problem.
Here is the code:
select productname
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'France')
and p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'Germany')
group by productname
So what I'm asking for is to maybe simplify this as much as possible but in basic ways, or show the way to skip that joins etc.
https://i.stack.imgur.com/UWlzn.png
select productname
from products p
join [Order Details] od on p.productid=od.ProductID
join orders o on od.OrderID=o.OrderID
join customers c on o.CustomerID=c.CustomerID
where c.Country in ('France', 'Germany')
group by productname
having COUNT(DISTINCT c.Country) = 2;
Is there a way to answer this question without using joins?
Write a query that finds, for each customer X, another customer Y who has ordered at least one product in common with X. Find all such pairs of Customers (X, Y) and against each pair, the number of overlapping products. The query should thus have three columns. Order the results by the number of overlapping products.
The question uses the https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
database.
Using joins, I can answer the question like this:
O2.CustomerID AS Cust2,
COUNT(*) AS OverlappingProd
FROM (SELECT O.CustomerID, OD.ProductID
FROM Orders AS O
JOIN OrderDetails AS OD
ON OD.orderid = o.orderid) AS O1
JOIN(SELECT O.CustomerID, OD.ProductID
FROM Orders AS O
JOIN OrderDetails AS OD
ON OD.orderid = o.orderid) AS O2
ON O2.ProductID = O1.ProductID
AND O2.CustomerID > O1.CustomerID
GROUP BY
O1.CustomerID,
O2.CustomerID
ORDER BY COUNT(*) DESC;
Is there a way to answer it not using the JOIN function? Thank you for your time and consideration.
I cannot think of a way of doing this without any joins. You can express this in SQL using cross join and exists, so the following comes close:
select c1.customerid, c2.customerid,
(select count(*)
from products p
where exists (select 1
from orderdetails od
where od.productid = p.productid and
exists (select 1
from orders o
where o.orderid = od.orderid and
o.customerid = c1.customerid
)
) and
exists (select 1
from orderdetails od
where od.productid = p.productid and
exists (select 1
from orders o
where o.orderid = od.orderid and
o.customerid = c2.customerid
)
)
) as num_products
from customers c1 cross join
customers c2
where c1.customerid < c2.customerid;
Although this syntax will work in many databases, the nested correlation clauses are not supported in MySQL. So, even accepting the CROSS JOIN, this does not work in MySQL.
The reason why a JOIN seems necessary is to get two independent customer ids in the result set.
I need to select the customer and product code and the date on which the order was made, but I'm having some trouble with the join orders.
My SQL select:
select c.customerNumber, p.productCode, o.orderDate as data_compra
from customers as c inner join orders as o
inner join products as p
where p.productCode =
any (
select p2.productCode from products as p2
inner join orders as o
inner join orderdetails as odt
where o.orderNumber = odt.orderNumber and
p2.productCode = odt.productCode
)
and o.orderNumber =
any (
select o2.orderNumber from orders as o2
inner join orderdetails as odt
where o.orderNumber = odt.orderNumber and
p.productCode = odt.productCode
)
Two simple joins should do what you want:
select
c.customerNumber,
d.productCode,
o.orderDate
from customer c
join orders o on o.customerNumber = c.customerNumber
join orderdetails d on d.orderNumber = o.orderNumber
In the code you're asking to inner join two tables but not specifying the relationship. You need to do so SQL can relate and match the rows in each table.
You do this with the ON keyword.
I suggest you watch this video and read this article before continuing
I have added a variable in SSIS and I am trying to add tha variable in my expression query. But it says Expression cannot be evaluated Below is my expression query. Please help if you know where I am making mistakes.
SQL Query
SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID = #CompanyID
Expression Format
"SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID ="+ #[User::intCompanyID]
Let me know if I am doing it right.
I found that casting the numeric values to String or Unicode works. so maybe try
ISNULL(c.IsResult,0) = 0 and o.CompanyID ="+ (DT_WSTR,20) #[User::intCompanyID]
Have you tried the following
"SELECT c.CustomerName, o.OrderID from Customers c
INNER JOIN Orders o ON c.CustomerID=o.CustomerID
Inner Join OrderDetails od ON od.OrderId = o.OrderID
Inner Join Products p on p.ProductID = od.ProductID
where ISNULL(c.IsResult,0) = 0 and o.CompanyID = ?"
NOTE: The image is from a different example
Click on the "Parameters" button and you can then selected your parameter from there.
Hope this helps.
//Personal understanding, not a hw assignment
So in the sample db northwind from MS there are the tables: orders o, [order details] od, customers c
o has orderID, customerID (inc. duplicates)
od has orderID (inc. duplicates), unitprice, quantity, discount
c has customerID, companyName
roughly speaking,
I want to join on
o.customerID = c.customerID; selecting companyName ///
join on o.orderID = od.orderID; selecting unitprice, quantity, discount
my end goal is to
sum(q (up - d)) AS 'Order Total' group by od.orderID then
sum(Order Total) group by companyName(?)
My main issue is not know how/what to join properly though.
Thanks in advance
Check out your scenario on SQL Fiddle
SELECT comp.`company_name` AS 'company',COUNT(DISTINCT o.id_sales_order) AS 'total_orders',SUM(`unit_price`) AS 'grand_total'
FROM sales_order AS o
LEFT JOIN sales_order_item AS od ON od.fk_sales_order = o.id_sales_order
LEFT JOIN customer AS c ON c.id_customer = o.fk_customer
LEFT JOIN company AS comp ON comp.id_company = c.fk_company_id
GROUP BY comp.`company_name`
hope this what you are looking for
Assuming that you create the correct Select statement as you required, the join part should be something like:
From Orders o join Order_Detail od on o.orderID = od.orderID
join Customer c on o.customerID = c.customerID
Types of join can be: join, inner join, right/left join. It depends on you what do you want to achive i.e if you want to exclude/include null references. But the structure is the same.