Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created January 18, 2013 04:40
Show Gist options
  • Select an option

  • Save oojacoboo/4562373 to your computer and use it in GitHub Desktop.

Select an option

Save oojacoboo/4562373 to your computer and use it in GitHub Desktop.
SELECT
*,
(@runningTotal := @runningTotal + t1.amount) AS balance
FROM
(SELECT *
FROM `transaction` AS t
WHERE t.company_id = :companyId
AND t.tenant_id = :tenantId
AND t.created_at > :startDate
AND t.deleted_at IS NULL
) AS t1
ORDER BY t1.id, t1.created_at /*must be ascending for calculating balance*/';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment