Show recurrent clients in this query - mysql

I have a query that shows me the amount of client per amount of orders placed.
select quantidade_pedidos, count(*)
from (select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id
) x
group by quantidade_pedidos;
I want to make clients that have placed 2 orders be listed in 1, clients that have placed 3 orders be listed in 2 etc as well.
Now it only groups by number of orders:
1 order - 700 clients
2 orders - 300 clients
3 orders - 100 clients
Those 300 clients must appear in the 1 order to because they placed 1 order and 2 orders and those 100 clients must appear in the 2 orders group because they placed two orders.
How can I correct this query?
Sorry if it got a little bit confusing.
Thanks

If I understand then what you want is to also group by the number of orders by adding a second group by.
select quantidade_pedidos, count(*) as order_count
from (select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id
) x
group by quantidade_pedidos, order_count;

Related

GROUP BY within functions

I'm currently stuck with MySQL
SELECT
c.id_customer,
ROUND(SUM((o.commision_main + o.commision_delivery)/(
SELECT COUNT(o.id_order)
FROM orders AS o
INNER JOIN customers AS c
ON o.id_customer = c.id_customer
GROUP BY)), 2) AS profit
FROM orders AS o
INNER JOIN customers AS c
ON o.id_customer = c.id_customer
GROUP BY c.id_customer
I would love to make this SQL to use the COUNT for each id_customer that was GROUP BYed as well.
Picture of result And now it takes COUNT for every id_customer so it divides everything by 6 instead of 3 for id_customer 3, 2 for id_customer 1 and 1 for id_customer 66.
Help would be appreciated!
Your query seems needlessly complex, I am pretty sure you can get the result you are after with simply
SELECT o.id_customer,
ROUND(AVG(o.commision_main + o.commision_delivery) , 2) AS Profit
FROM Orders AS o
GROUP BY o.id_customer;
You should avoid the subquery for count() and use count() directly in main query
SELECT
c.id_customer,
ROUND(SUM((o.commision_main + o.commision_delivery)/COUNT(o.id_order)), 2) AS profit
FROM orders AS o
INNER JOIN customers AS c ON o.id_customer = c.id_customer
GROUP BY c.id_customer

MySQL How to GROUP BY counted column?

I have two tables users, orders
each table has below column
users(table)
id
orders(table)
user_id
How can i get the number of users whose order count is 1,2,3,4 ....n?
Like this?
users count | order count
999 | 1
100 | 2
80 | 3
70 | 4
60 | 5
50 | 6
What I have been trying so far is
SELECT cnt.uid as u_cnt, cnt.ocnt as or_cnt
FROM (
SELECT u.id as uid, COUNT(o.id) as o_cnt
FROM users as u
INNER JOIN orders o on u.id = o.user_id
) as cnt;
GROUP BY or_cnt
BUT I get only 1 u_cnt and summed or_cnt
You need two levels of group by clauses here: First, you need to group by user and count the number of orders each user has. Then, you need to take that result, group by the number of orders and count how many users have such an order count.
The easiest way to achieve this is probably with a subquery, where both the inner and outer query have a group by clause:
SELECT cnt.ocnt as or_cnt, COUNT(*) as user_count
FROM (
SELECT u.id as uid, COUNT(o.id) as o_cnt
FROM users as u
INNER JOIN orders o on u.id = o.user_id
GROUP BY u.id -- This was missing in your original query
) as cnt
GROUP BY or_cnt
You can use two levels of aggregation. More importantly, you do not need a JOIN. All the information you need is in orders:
SELECT o_cnt, COUNT(*) as user_count
FROM (SELECT o.user_id, COUNT(*) as o_cnt
FROM orders o
GROUP BY o.user_id
) u
GROUP BY o_cnt
ORDER BY o_cnt;

SQL to get top 3 most ordered products for every user by quantity sold

I am working on a school project the idea of which is an eCommerce site for selling beers.
I need to get the top 3 most ordered products for every individual user that has ordered something. My database includes 4 tables: users, products, orders and order_detail. The diagram is:
I think I will have to join all 4 of the tables to get that info, but I can't figure out the correct way to do it. Here is what I have generated:
SELECT u.username, p.`id` AS productId, p.`name`, od.`quantity` AS quantity
FROM `order_detail` AS od
INNER JOIN `products` AS p
INNER JOIN `users` AS u
ON od.`product_id` = p.`id`
GROUP BY od.`order_id`, p.name
ORDER BY od.`quantity` DESC, p.`name` ASC
The database script: https://pastebin.com/BvQLGqur
In order to limit the rows returned to show just the top 3 products ordered for each user, you'll need to use a sub-query with a limit clause on the order_detail.
Other than that, it just a simple join between the 4 tables.
SELECT
a.`user_name`,
d.`id` as `product_id`,
d.`name` as `product_name`,
SUM(c1.`quantity`) as `total_quantity`,
d.`price` as `product_price`,
d.`price` * SUM(c1.`quantity`) as `total_spent`
FROM `users` a
JOIN `orders` b
ON b.`user_id` = a.`id`
JOIN (SELECT c.`order_id`, c.`product_id`, SUM(c.`quantity`) as `num_ordered`
FROM `order_detail` c
ORDER BY `num_ordered` DESC
LIMIT 3) as c1
ON c1.`order_id` = b.`id`
JOIN `products` d
ON d.`id` = c1.`product_id`
GROUP BY a.`id`,d.`product_id`
ORDER BY a.`user_name`,`total_quantity` DESC, d.`name`;

How to get first value from this sql query

Im trying to get by subquery clientId of customer with most orders but only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
SELECT a.ClientName
FROM Clients as a
INNER JOIN Orders as b
ON a.Id=b.ClientId
WHERE b.ClientId
IN(SELECT b.ClientId,COUNT( b.ClientId) as MAKS FROM Orders as b
GROUP BY b.ClientId ORDER BY MAKS DESC)
Do we have some tools to handle this and how can i optimize this query? Thanks in advance.
You don't really need the inner join because you are asking for an ID that is the same in both tables,
SELECT ClientName FROM Clients
WHERE Id = (SELECT TOP 1 ClientId FROM Orders
GROUP BY ClientId
ORDER BY COUNT(ClientId) DESC)
Using Top 1, Count and Group By
SQL Server
SELECT Top 1 a.ClientName , count(b.orders_id) TotalOrders
FROM Clients as a
INNER JOIN Orders as b
ON a.Id=b.ClientId
GROUP BY a.client_name
order by TotalOrders desc
MySQL
SELECT a.ClientName , count(b.orders_id) TotalOrders
FROM Clients as a
INNER JOIN Orders as b
ON a.Id=b.ClientId
GROUP BY a.client_name
order by TotalOrders desc
LIMIT 1

mysql: list number of orders all customers made

I have 2 simple tables like customers and orders where a customer can have 0 to n orders. I would like to have a list like:
10 customers made 0 orders
4 customers made 1 order
5 customers made 2 orders
2 customers made 3 orders
... and so on. So far I have:
SELECT customers.id FROM customers
LEFT JOIN orders ON orders.customerId = customers.id
HAVING COUNT(orders.id) = 1;
which lists me all customers who made 1 order, but as I said I need a list with all possebilities.
SELECT COUNT(*), t.orderCount
FROM
(
SELECT u.id, COUNT(o.id) AS orderCount
FROM users u LEFT JOIN orders o
ON u.id = o.userId
GROUP BY u.id
) t
GROUP BY t.orderCount
The inner query will give each user and his order total:
user total
1 2
2 3
3 2
4 3
The outer query does a GROUP BY this total to count the number of customers who share the total:
# customers total
2 2
2 3
You can use aggregate function and group bu
SELECT ifnull(count(orders.id),0), users.id FROM users
LEFT JOIN orders ON orders.userId = users.id
group by users.id