Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created July 3, 2012 05:48
Show Gist options
  • Save oojacoboo/3037942 to your computer and use it in GitHub Desktop.
Save oojacoboo/3037942 to your computer and use it in GitHub Desktop.
SELECT t.`id`, t.`reference_id`, t.`tenant_id`, t.`amount`,
(
SELECT SUM(t.`amount`)
FROM `transaction` t
) AS total
FROM `transaction` t
WHERE total > 0
GROUP BY t.`reference_id`;
@oojacoboo
Copy link
Author

SELECT t.id, t.reference_id, t.tenant_id, t.amount
FROM transaction t
WHERE total =
(
SELECT total
FROM (
SELECT SUM(t.amount) as total
FROM transactions t
) t
GROUP BY t.reference_id
WHERE total > 0 ) t

@oojacoboo
Copy link
Author

SELECT t.id, t.reference_id, t.tenant_id, t.amount, SUM(t.amount) total
FROM transaction t
GROUP BY t.reference_id
HAVING total > 0

@oojacoboo
Copy link
Author

SELECT t.id, t.reference_id, t.tenant_id, t.amount,
(
SELECT SUM(t.amount)
FROM transaction t
WHERE t.deleted_at IS NOT NULL
) AS total
FROM transaction t
GROUP BY t.reference_id
HAVING total > 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment