SQL join and sum of elements

There are two tables.

of users

+--+----+
|id|name|
+--+----+
 1   A    
 2   B

Orders

+--+--------+-------+-------+
|id|subtotal|created|user_id|
+--+--------+-------+-------+
 1  10       1000001   1
 2  20       1000002   1
 3  10       1000003   2
 4  10       1000005   1

The idea is to receive AVG, SUMand the last order created from users.

SELECT
  users.name,
  users.phone,
  SUM(a.subtotal),
  COALESCE(a.created, NULL)
  FROM users
LEFT JOIN
  (
  SELECT
    orders.id,
    orders.subtotal,
    orders.user_id,
    orders.created
  FROM
    orders 
  JOIN(
    SELECT MAX(i.created) created, i.user_id 
      FROM orders i
      GROUP BY i.user_id
  )AS j ON(j.user_id = orders.user_id AND orders.created = j.created) GROUP BY orders.user_id
) AS a ON users.id = a.user_id
GROUP BY users.id

For example, an SQL query should return this:

+--+----+---+--------+
|id|name|sum|date    |
+--+----+---+--------+
 1   A    40  1000005
 2   B    10  1000003

But SQL above could not calculate the sum. What am I missing?

+4
source share
1 answer

Your request seems too complicated. How about this?

SELECT u.id, u.name, SUM(o.subtotal), MAX(o.created)
FROM users u LEFT JOIN
     orders o
     ON u.id = o.user_id
GROUP BY u.id, u.name;

In MySQL, it is especially important to avoid unnecessary subqueries in a sentence FROM. They actually materialize and may hamper the use of indexes for performance.

+7
source

Source: https://habr.com/ru/post/1653373/


All Articles