I have a problem that I have searched for and can't find answers that offer any help.
There are 3 tables: orders, customers, and teams. Each order has a customer and a team. My problem is that for each customer, I am trying to get their total orders, and then the team that was assigned to their last order.
My query so far (works fine):
SELECT c.id, MAX(o.timestamp) AS "Last Order", COUNT(o.id) AS "Total Orders", SUM(o.price) AS "Total Spend"
FROM orders o
LEFT JOIN customers c ON o.customer = c.id
WHERE o.timestamp > '2012-01-01 00:00:00'
GROUP BY c.id
ORDER BY c.id
I can't figure out how to join the team.name for the last order they placed, without affecting the rest of the query that I have.
Sample Tables and Desired Result:
Customers:
╔════╦═════════════╗
║ ID ║ NAME ║
╠════╬═════════════╣
║ 1 ║ John Smith ║
║ 2 ║ Jim Jimmers ║
╚════╩═════════════╝
Teams:
╔════╦═════════════╗
║ ID ║ NAME ║
╠════╬═════════════╣
║ 1 ║ Red ║
║ 2 ║ Blue ║
╚════╩═════════════╝
Orders:
╔═══════╦═════════════════════╦══════════╦══════╦═══════╗
║ ID ║ TIMESTAMP ║ CUSTOMER ║ TEAM ║ VALUE ║
╠═══════╬═════════════════════╬══════════╬══════╬═══════╣
║ 34656 ║ 2012-03-04 14:23:44 ║ 1 ║ 2 ║ 20 ║
║ 37345 ║ 2012-04-12 11:32:07 ║ 2 ║ 2 ║ 25 ║
║ 38220 ║ 2012-07-18 09:53:54 ║ 1 ║ 2 ║ 15 ║
║ 39496 ║ 2012-07-03 10:11:32 ║ 1 ║ 1 ║ 38 ║
║ 41752 ║ 2012-09-17 19:34:05 ║ 1 ║ 2 ║ 9 ║
║ 43734 ║ 2012-11-23 07:52:12 ║ 2 ║ 1 ║ 20 ║
╚═══════╩═════════════════════╩══════════╩══════╩═══════╝
How do I select a result like:
╔════╦═════════════╦═════════════════════╦═══════════╦══════════════╦═════════════╗
║ ID ║ NAME ║ LAST_ORDER ║ TEAM_NAME ║ TOTAL_ORDERS ║ TOTAL_SPEND ║
╠════╬═════════════╬═════════════════════╬═══════════╬══════════════╬═════════════╣
║ 1 ║ John Smith ║ 2012-09-17 19:34:05 ║ Blue ║ 4 ║ 82 ║
║ 2 ║ Jim Jimmers ║ 2012-11-23 07:52:12 ║ Red ║ 2 ║ 45 ║
╚════╩═════════════╩═════════════════════╩═══════════╩══════════════╩═════════════╝
You can use a query similar to this:
select c.id,
c.name,
o1.Last_Order,
t.name Team_Name,
o3.Total_Orders,
o3.Total_Spend
from customers c
inner join
(
select max(timestamp) Last_Order,
max(id) Last_id,
customer
from orders
WHERE timestamp > '2012-01-01 00:00:00'
group by customer
) o1
on c.id = o1.customer
inner join orders o2
on c.id = o2.customer
and o1.Last_Order = o2.timestamp
and o1.Last_id = o2.id
inner join teams t
on o2.team = t.id
inner join
(
select count(team) Total_Orders,
sum(value) Total_Spend,
customer
from orders
WHERE timestamp > '2012-01-01 00:00:00'
group by customer
) o3
on c.id = o3.customer
WHERE o2.timestamp > '2012-01-01 00:00:00'
See SQL Fiddle with Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With