MySQL: how many different customers ordered - mysql

i have a question with the data base Northwind
i need to Write a query that will retrieve for each Product the product name and how many different customers ordered it when the number of customers who ordered the same product must be at least 2, ie at least two different customers ordered the product
i did something like that but idk how to continue
thx
SELECT p.ProductID,p.ProductName ,count(CustomerId)
FROM Products p
join `order details` oon p.ProductID=o.ProductID AND Quantity>=2
join Orders kk where kk.OrderID=o.OrderID
group by p.ProductID
order by p.ProductID

The question says that the number of customers must be at least 2, not the quantity that they ordered. You can do this in a HAVING clause that compares the aggregate count.
You need to use COUNT(DISTINCT CustomerId) so you don't count the same customer multiple times for each order.
SELECT p.ProductID,p.ProductName ,count(DISTINCT kk.CustomerId) AS numCustomers
FROM Products p
join `order details` on p.ProductID=o.ProductID
join Orders kk on kk.OrderID=o.OrderID
group by p.ProductID
order by p.ProductID
HAVING numCustomers >= 2

Related

Select From multiple tables and relate with COUNT

first sorry, i don't have fluid english.
I want select 3 rows in 3 different tables, two of them without Foreing Key/relation.
Need to select amount of customers in each store, and total amount of payments in these stores in one query.
Here are the tables:
Customers
Stores
Payments
I have tried these querys to get payments for each store and customers for each store, but don't know how can unify in one query:
Payments/store
SELECT count(a.payment_id) as alquileres, b.store_id
FROM customer b, payment a
WHERE a.customer_id = b.customer_id
GROUP BY b.store_id;
customers/store
SELECT count(customer_id), store_id
FROM customer
GROUP BY store_id;
But when I add count(customer_id) in unique query don't have same results.
You can use one query:
select c.store_id,
count(distinct c.customer_id) as num_customers,
count(p.payment_id) as num_payments
from customers c left join
payments p
on p.customer_id = c.customer_id
group by c.store_id;

How to count from a single table whilst performing multiple joins

I was asked the following question:
Find the 3 vendors that supply our most famous products by number of units sold, not number of units ordered. Rank them first to third.
The database used is the online classicmodels db: http://mysqltutorial.org//tryit/
This is what I have done, I cannot seem to count the payments only and group by vendors. The count I get back is a product of all the joins.
SELECT p.productvendor FROM products AS p
INNER JOIN orderdetails AS od ON p.productcode = od.productcode
INNER JOIN orders AS o ON o.ordernumber = od.ordernumber
INNER JOIN customers AS c ON c.customernumber = o.customernumber
INNER JOIN payments AS py ON py.customernumber = c.customernumber
GROUP BY p.productvendor
ORDER BY COUNT(p.productvendor) DESC
LIMIT 3
Try to break the problem into smaller queries and then try to optimize the joins.
select count(p.productvendor) as product_count, p.productvendor from (JOINS) group by p.productvendor
-- this should give count by vendor. you can use sum(orderdetails.qty) also in case you
So the problem is reduced to be in the JOINS
JOINS ( has to be a collection of individual product sales)

inner-join mysql joining 3 tables

i am having problems using an inner join for 3 tables..
i need to display cust_id, the customer forename and surname, the product name<-(from products table) and date of sale<--(from sales table), also i need to display in order of the most recent dates first.
this is what i have got so far
enter SELECT
customers.cust_id,
customers.forename,
customers.surname,
products.prod_name,
sales.Date_of_sale
FROM
customers
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id; here
id really appreciate it if you could help me here, thank you..
Just add one more JOIN to the products table and include an ORDER BY clause:
SELECT
c.cust_id,
c.forename,
c.surname,
p.prod_name,
s.Date_of_sale
FROM customers c
INNER JOIN sales s ON c.cust_id = s.cust_id
INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC
A Visual Explanation of SQL Joins
I think the problem is in your FROM parameter.
You specified only customer.
SELECT customers.cust_id, customers.forename, customers.surname, products.prod_name, sales.Date_of_sale
FROM
customers , products , sales
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id;

MySQL query with JOINS and GROUP BY

I'm building a MySQL query but I can't seem to get it right.
I have four tables:
- customers
- orders
- sales_rates
- purchase_rates
There is a 1:n relation 'customernr' between customers and orders.
There is a 1:n relation 'ordernr' between orders and sales_rates.
There is a 1:n relation 'ordernr' between orders and purchase_rates.
What I would like to do is produce an output of all customers with their total purchase and sales amounts.
So far I have the following query.
SELECT c.customernr, c.customer_name, SUM(sr.sales_price) AS sales_price, SUM(pr.purchase_price) AS purchase_price
FROM orders o, customers c, sales_rates sr, purchase_rates pr
WHERE o.customernr = c.customernr
AND o.ordernr = sr.ordernr
AND o.ordernr = pr.ordernr
GROUP BY c.customer_name
The result of the sales_price and purchase_price is far too high. I seem to be getting double counts. What am I doing wrong? Is it possible to perform this in a single query?
Thank for your response!
It seems that the problem is that when you join the orders table to the tables with sales rates and purchase rates, you are getting the cartesian product of these two latter tables. I.e each row in these two tables are repeated once for each correponding row in the other table. The following query should solve this problem by summing the rates for each order before joining the sales rates and purchase rates to the other tables:
SELECT c.customernr, c.customer_name,
SUM(sr.sales_price) AS sales_price,
SUM(pr.purchase_price) AS purchase_price
FROM customers c
INNER JOIN orders o
ON o.customernr = c.customernr
LEFT JOIN (SELECT ordernr, SUM sales_price) AS sales_price
FROM sales_rates
GROUP BY ordernr) sr
ON sr.ordernr = o.ordernr
LEFT JOIN (SELECT ordernr, SUM(purchase_price) AS purchase_price
FROM purchase_rates
GROUP BY ordernr) pr
ON pr.ordernr = o.ordernr
GROUP BY c.customernr, c.customer_name;
It doesn't look like you are grouping by the customer. C.customerid or something like that.

A SQL query that does not work for getting number of orders in a table

This is a part of SQL coding project of database.
I need to design a single table to hold orders made by customers.
Assume that customers' details (e.g. names) are stored in another table.
Using the table that I designed for orders, and this assumed other table, write
a single SQL query that can give the number of orders for every customer, one line per customer.
The “Orders” table
Order_ID Order_NO Customer_ID
1 8088 3
2 9632 1
3 1272 4
4 6037 1
Assume that the customer names and other details (address, phone numbers, emails) are stored in the “Customers” table.
My SQL:
SELECT Customers.FirstName, Customers.FirstName, Orders.OrderNo
FROM Customers
FULL JOIN Orders
ON Customers.Customer_ID = Orders.Customer_ID
ORDER BY Customers.LastName
Are there something wring with it ?
Change the query to inner join
SELECT
Customers.FirstName
, Customers.FirstName
, Orders.OrderNo
FROM
Customers
INNER JOIN Orders ON Customers.Customer_ID = Orders.Customer_ID
ORDER BY
Customers.LastName
Query for Ordercount for customer
SELECT
Customers.LastName
, COUNT(Orders.Order_Id)
FROM
Customers
INNER JOIN Orders ON Customers.Customer_ID = Orders.Customer_ID
GROUP BY
Customers.LastName
ORDER BY
Customers.LastName
Full join
http://www.w3schools.com/sql/sql_join_full.asp
Inner join
http://www.w3schools.com/sql/sql_join_inner.asp
If you just want to COUNT the orders you can do this:
SELECT
Customers.FirstName,
(
SELECT
COUNT(*)
FROM
Orders
WHERE
Orders.Customer_ID=Customers.Customer_ID
) AS NbrOfOrders
FROM
Customers
References:
12.15.1. GROUP BY (Aggregate) Functions
Don't forget COUNT and GROUP BY.