Skip to content

Instantly share code, notes, and snippets.

@mattm
Last active April 23, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattm/a36371071b51ce98da906d5657ef1dd8 to your computer and use it in GitHub Desktop.
Save mattm/a36371071b51ce98da906d5657ef1dd8 to your computer and use it in GitHub Desktop.
Charges using CROSS JOIN
SELECT *, ROUND(amount / total * 100) AS percent
FROM charges
CROSS JOIN (SELECT SUM(amount) AS total FROM charges) t;
+----+--------+-------+---------+
| id | amount | total | percent |
+----+--------+-------+---------+
| 1 | 18.00 | 60.00 | 30 |
| 2 | 15.00 | 60.00 | 25 |
| 3 | 27.00 | 60.00 | 45 |
+----+--------+-------+---------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment