MySQL selecting names of customer with most purchase - mysql

I have 5 tables connected with each other in the image
[![Image1][1]][1]
I have tried writing query
SELECT lastname,firstname
FROM customer,purchase_order
where customer.customer_id=purchase_order.customer_id
What parameter should I consider for most purchase?

You can solve that by adjusting the select clause (remove order_id).
Then you will need a HAVING clause to implement the condition to only show the customers with the most purchase orders.

SELECT lastname,firstname
FROM customer
INNER JOIN purchase_order
ON customer.customer_id=purchase_order.customer_id
GROUP BY customer_id
ORDER BY COUNT(order_id) DESC LIMIT 1;

select lastname, firstname from customer c
inner join (
select customer_id, count(1) from purchase_order
group by 1
order by 2 desc limit 1) most
/* the above sub-query named as 'most' fetched the customer id with the maximum
number of orders */
on c.customer_id = most.customer_id; -- this is a simple join of customers table with the table generated from sub-query above

SELECT c.firstname,c.lastname
FROM customer c
JOIN purchase_order po
ON c.customer_id = po.customer_id
GROUP BY c.firstname,c.lastname
ORDER BY COUNT(po.order_id) DESC LIMIT 1;

Related

MySQL subquery to get a value from a table based on id from another table

I'm trying to write up a subquery in order to retrieve a client name from the clients table by using a client_id from the orders table.
1st query:
SELECT client_id, order_id, deadline, state FROM orders GROUP BY order_id
2nd query:
SELECT name FROM clients WHERE id=:client_id
What would the 2nd query look like as a subquery into the first in order to display final results like the following, adding the name from the clients table based on each client_id from orders table matching id from the clients table:
client_id - from orders table
order_id - from orders table
deadline - from orders table
state - from orders table
name - from clients table
grouped by order_id from orders table
If I understand you correctly, you need to get second table's name value in your result set. Then you have to get values from 2 tables. Using subquery is the one solution:
SELECT o.client_id, o.order_id, o.deadline, o.state, c.name
FROM orders o, clients c
WHERE (c.id = o.client_id)
GROUP BY o.order_id;
JOIN Clause
SELECT o.client_id,
o.order_id,
o.deadline,
o.state,
c.name
FROM orders o
join clients c on c.id = o.client_id
GROUP BY o.order_id
GROUP BY
SQL-92 and earlier does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause. You'd better use this:
SELECT o.client_id,
o.order_id,
o.deadline,
o.state,
c.name
FROM orders o
join clients c on c.id = o.client_id
GROUP BY o.client_id,
o.order_id,
o.deadline,
o.state,
c.name

how to conbine the following two query

the following query no.1 and query no.2 are working each line..
1.
SELECT app_user.au_userid, SUM(order_detail.od_quantity), (order_detail.od_quantity*SUM(order_detail.od_subtotal)) AS total_amount FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id JOIN order_detail ON orders.o_id = order_detail.od_order_id GROUP BY app_user.au_userid
2.
SELECT COUNT (app_user.au_id) FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id GROUP BY app_user.au_id
the following query no.3 doesn't working..
3.
SELECT app_user.au_userid, (SELECT COUNT (app_user.au_id) FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id GROUP BY app_user.au_id) AS total, SUM (order_detail.od_quantity), (order_detail.od_quantity*SUM (order_detail.od_subtotal)) AS total_amount FROM app_user JOIN orders ON app_user.au_id = orders.o_au_id JOIN order_detail ON orders.o_id = order_detail.od_order_id GROUP BY app_user.au_userid
The simplest method uses COUNT(distinct):
SELECT o.o_au_id, COUNT (DISTINCT o.o_id) AS num_orders,
SUM(od.od_quantity),
SUM(od.od_quantity * od.od_subtotal) AS total_amount
FROM orders o JOIN
order_detail od
ON o.o_id = od.od_order_id
GROUP BY o.o_au_id;
Notes:
You don't need app_users, because you have the user id in the orders table.
Table aliases make the query easier to write and to read.
For calculating the total amount, you should be doing the multiplication before the summation.
To get the number of orders, use count(distinct).

MySQL - Select last record from second table matching with first table

I have two tables customers and orders, below is the structure.
Table - customers
id
customer_name
Table - orders
id
order_id
customer_id
customers table have customers records and orders table have orders placed by customers,
customer_id in orders table is linked to the id field of customers table.
Now one customer can have zero or one or more than one orders, i want to get the last order placed by customers only.
when i run the following query a simple invisible join, it returns all the orders by the customer
SELECT customers.customer_name,orders.order_id FROM orders,customers WHERE orders.customer_id=customers.id
I have also tried different JOIN statements but cannot get the last order by the customer, i want to get it in one SQL query for all customers.
Thank you in advance for your help.
In MySQL there is just few ways to make it work (that I now actually). The first one is sort your table as desc before the join:
SELECT c.customer_name, o.customer_id, o.order_id,o.id FROM customers c
INNER JOIN orders o
ON o.id = (SELECT id FROM orders WHERE customer_id = c.id ORDER BY id DESC LIMIT 1)
Using in real time is the only way to get it done, but if you need to make some join on not real time you can create a temporary table or a alias table sorting it to make your select, like this:
CREATE TABLE tmp_your_table AS
SELECT * FROM orders ORDER BY id DESC
So now you are able to make this join work:
SELECT c.customer_name, o.customer_id, o.order_id,o.id FROM customers c
INNER JOIN tmp_your_table o ON o.id = tmp_your_table.id
Try this query
SELECT
c.customer_name,
max(o.order_id)
FROM
customers c
INNER JOIN
orders o
ON
o.customer_id = c.id
GROUP BY
c.customer_name
You don't have any date field in the order table so assuming the latest order will be the one which has max(order_id).
Try this query
SELECT
c.customer_name,
o.order_id
FROM
customers c
INNER JOIN
orders o
ON
o.customer_id = c.id
ORDER BY
o.id desc
LIMIT 1;

SQL max of a count involving 3 tables

I have one table Customers with CustomerID and PhoneNumber, the second table is Orders that has CustomerId and OrderNumber and a third table OrderDetails that has OrderNumber, PriceOfOneUnit and UnitsOrdered. I need to find the PhoneNumber for the customer who placed the largest order (PriceOfOneUnit * UnitsOrdered). Doing a count(PriceOfOneUnit*UnitsOrdered) as A1 and then `Group By CustomerId Order By A1 DESC LIMIT 1 is evidently not working after joining the 3 tables. Can anyone help.
If we take you at your word, and what you want is the biggest single line-item rather than the largest order, you can find the largest line-item and then find the order to which it belongs and then the customer who placed that order. You can use a dummy aggregate function to pull back the order id from orderDetails.
EDIT:
OK, for someone just starting out, I think it can be clearer to think in terms of Venn diagrams and use what are called Inline Views and subqueries:
select customername, phone
from customer
inner join
(
select o.id, customerid from orders o
inner join
(
select od.orderid from orderdetail od
where (od.qty * od.itemprice) =
(
select max(od.qty * od.itemprice)
from orderdetail as od
)
) as biggestorder
on o.id = biggestorder.orderid
) as X
on customer.id = X.customerid
Each of the queries inside parentheses returns a set that can be joined/intersected with other sets.
Give this a try,
SELECT cus.CustomerId, cus.PhoneNumber
FROM Customers cus
INNER JOIN Orders a
ON cus.CustomerId = a.CustomerId
INNER JOIN OrderDetails b
On a.OrderNumber = b.OrderNumber
GROUP BY cus.CustomerId, cus.PhoneNumber
HAVING SUM(b.PriceOfOneUnit * b.UnitsOrdered) =
(
SELECT SUM(b.PriceOfOneUnit * b.UnitsOrdered) totalOrdersAmount
FROM Orders aa
INNER JOIN OrderDetails bb
On aa.OrderNumber = bb.OrderNumber
GROUP BY aa.CustomerId
ORDER BY totalOrdersAmount DESC
LIMIT 1
)

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.