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
Related
I have 2 query:
SELECT CustomerID,count(b.BookingStatus) as 'NotComplete'
FROM Booking b, Customer c
WHERE c.CustomerID=b.BookingCustomerID
AND(b.BookingStatus='Pending'
OR b.BookingStatus='OTW')
GROUP BY c.CustomerID
SELECT c.CustomerID, r.*
FROM Customer c,Regular r
WHERE c.CustomerID=r.RegularCID
Result:
1st query
2nd query
How to combine these 2 result together?
also, display the zero(count) as well.
Thanks!
this is what I get after few hours of trying..obviously it's not what I want..
SELECT c.CustomerID,count(b.BookingStatus) as 'NotComplete',r.RegularID
FROM Booking b, Customer c
JOIN Regular r on r.RegularCID=c.CustomerID
WHERE c.CustomerID=b.BookingCustomerID
AND (b.BookingStatus='Pending'
or b.BookingStatus='OTW'
or b.BookingStatus='Started'
or b.BookingStatus='Unclaimed'
or b.BookingStatus='Confirmed')
GROUP by r.RegularID
You can JOIN to the Regular table and then LEFT JOIN to a derived table of counts in the Booking table. We do it this way to avoid having to GROUP BY all the columns in the Regular table:
SELECT c.CustomerID, r.*, b.NotComplete
FROM Customer c
JOIN Regular r ON r.RegularCID = c.CustomerID
LEFT JOIN (SELECT BookingCustomerID, COUNT(*) AS NotComplete
FROM Booking
WHERE BookingStatus IN ('Pending', 'OTW', 'Started', 'Unclaimed', 'Confirmed')
GROUP BY BookingCustomerID) b ON b.BookingCustomerID = c.CustomerID
Use join on regular table, and subquery on your first select
SELECT t1.*, r.RegularCID FROM (
SELECT CustomerID,count(b.BookingStatus) as 'NotComplete',
FROM Booking b
INNER JOIN Customer c ON c.CustomerID=b.BookingCustomerID
WHERE (b.BookingStatus='Pending' OR b.BookingStatus='OTW')
GROUP BY c.CustomerID) t1
LEFT JOIN Regular r on r.CustomerID = t1.CustomerID
I have this query that needs to be corrected. So right now I have 90% of what I need but this last step has been giving me trouble. I know on 1996-07-04 employee #5 visited my site for the first time, and rightfully get the first timer count as 1. On 1996-07-11 I know employee #5 visited the site again but we do not want to count him again.
SELECT
Orders.OrderDate,
COUNT(OrderDetails.OrderDetailID) AS NumSessions,
COUNT(DISTINCT Orders.EmployeeID)
FROM OrderDetails
LEFT JOIN Orders
ON OrderDetails.OrderID=Orders.OrderID
GROUP BY Orders.OrderDate
EXPECTED:
DATE NumSessions FirstTimers
1996-07-04 3 1
1996-07-11 3 0
ACTUAL:
DATE NumSessions FirstTimers
1996-07-04 3 1
1996-07-11 3 1
Maybe something like this (I can't guarantee it's syntactically correct, though):
SELECT
Orders.OrderDate,
COUNT(OrderDetails.OrderDetailID) AS NumSessions,
COUNT(DISTINCT Orders.EmployeeID WHERE Orders.OrderDate = (
SELECT MIN(b.OrderDate) FROM Orders b WHERE Orders.EmployeeID = b.EmployeeID)
)
FROM OrderDetails
LEFT JOIN Orders
ON OrderDetails.OrderID=Orders.OrderID
GROUP BY Orders.OrderDate
But I don't know how this would do performance-wise. :)
This is a little complicated. One method is to use a subquery to calculate the first order date and to use that in the logic:
SELECT o.OrderDate,
COUNT(od.OrderDetailID) AS NumSessions,
SUM( o.OrderDate = oe.FirstOrderDate ) as FirstTimers
FROM Orders o LEFT JOIN
OrderDetails od
ON O.OrderId = od.OrderId LEFT JOIN
(SELECT o.EmloyeeID, MIN(OrderDate) as FirstOrderDate
FROM Orders o
GROUP BY OrderDate
) o
ON oe.EmployeeId = o.EmployeeId
GROUP BY o.OrderDate
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;
There title does not quite describe well what i need from the query.
#sgeddes helped me come up with the following query but the query needs some alteration to accomplish my needs. I also modified the query slightly to not Select deleted customers but i couldn't change much due to the way this query is written is out my SQL knowledge.
SELECT d.customer_id,d.fname,d.lname,d.isactive,
o.lastdate,
Count(o2.order_id) AS 'total_orders'
FROM customers d
LEFT JOIN (SELECT MAX(order_id) order_id, customer_id
FROM orders
GROUP BY customer_id) m on d.customer_id = m.customer_id
LEFT JOIN orders o on m.order_id = o.order_id
LEFT JOIN orders o2 on d.customer_id = o2.customer_id
AND o2.balance > 0 AND o2.isActive > -1
WHERE d.user_id =945766 AND d.isActive > -1
AND o2.customer_id IS NULL
GROUP BY d.customer_id
I need the three following requirements.
count orders for customer with isActive > -1 (-1 = deleted)
Customers not in orders table (customer with no orders).
Customers with isActive = 0 and their corresponding order count
so briefly all I need is customer with isActive = 0 and to get an actual # for total_orders column.
In my attempt to better help you understand my requirement I created a SqlFiddle.
Please see my SqlFiddle
If your requirement is what you mentioned in your 3 points then you can use below queries-
If you want separate query then use below-
SELECT d.customer_id, d.fname, COUNT(o.order_id) AS Total_Orders
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE d.isActive > -1
GROUP BY d.customer_id;
SELECT DISTINCT d.customer_id, d.fname
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE o.customer_id IS NULL;
SELECT d.customer_id, d.fname, COUNT(o.order_id) AS Total_Orders
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE d.isActive = 0
GROUP BY d.customer_id;
If you want to merge them by union then use below-
SELECT 'isactive>-1' AS 'status', d.customer_id, d.fname, COUNT(o.order_id) AS Total_Orders
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE d.isActive > -1
GROUP BY d.customer_id
UNION ALL
SELECT DISTINCT 'Customer without Order' AS 'status', d.customer_id, d.fname, COUNT(o.order_id) AS Total_Orders
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE o.customer_id IS NULL
UNION ALL
SELECT 'isactive=0' AS 'status', d.customer_id, d.fname, COUNT(o.order_id) AS Total_Orders
FROM customer d
LEFT JOIN orders o ON d.customer_id=o.customer_id
WHERE d.isActive = 0
GROUP BY d.customer_id;
Note: Your main query is trying fetch latest order which can be for any other purpose, so if you provide your exact requirement if different from it then someone can help you.
I am working on an mySQL assignment for school and I am stuck on a question. I am still new to mySQL. COUNT(o.customer_id) is not working the way I want. I want it to count the number of orders but it is counting all items. i.e. Customer 1 has 2 orders but it is returning 3 because one order has two items. I have three tables one with customers, another with orders than another with each item on each order. Ive posed my query below. Any help would be great.
SELECT email_address, COUNT(o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items ot
ON o.order_id = ot.order_id
GROUP BY o.customer_id
HAVING num_of_orders > 1
ORDER BY total DESC;
As simple as use Distinct reserved word:
SELECT email_address, COUNT(distinct o.order_id) AS num_of_orders
Looks like you want to count the DISTINCT number of orders. Add a DISTINCT into the COUNT. Although MySQL allows you to use the SELECT expression in the HAVING clause, it's not good practice to do so.
SELECT email_address, COUNT(DISTINCT o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items ot
ON o.order_id = ot.order_id
GROUP BY o.customer_id
HAVING COUNT(DISTINCT o.order_id) > 1
ORDER BY total DESC;
Just take out the join to items. All it is doing is duplicating rows when there are multiple items.
SELECT email_address, COUNT(o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY o.customer_id
HAVING COUNT(o.order_id) > 1
ORDER BY total DESC;