Guest User

Untitled

a guest
Dec 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.50 KB | None | 0 0
  1. SELECT COUNT(*)
  2. FROM
  3.     (SELECT a.name account_name, SUM(o.standard_qty)
  4.     standard_sum
  5.     FROM accounts a
  6.     JOIN orders o
  7.     ON a.id = o.account_id
  8.     GROUP BY account_name
  9.     ORDER BY standard_sum DESC
  10.     LIMIT 1) most_standard_qty
  11. JOIN
  12.     (SELECT a.name account_name, SUM(o.total) total_sum
  13.      FROM accounts a
  14.      JOIN orders o
  15.      ON a.id = o.account_id
  16.      GROUP BY a.name
  17.      ) total
  18.  ON most_standard_qty.account_name = total.account_name
  19.  HAVING total.total_sum > most_standard_qty.standard_sum
Advertisement
Add Comment
Please, Sign In to add comment