Created
January 21, 2013 20:44
-
-
Save stevenharman/4589186 to your computer and use it in GitHub Desktop.
Using Arel/ActiveRecord to execute a subquery, including a SUM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select fund_sums.total, fund_products.name | |
from ( | |
select sum(allocation_amount) total, fund_product_id | |
from transactions | |
where transactions.investor_id = 490 | |
group by fund_product_id | |
) fund_sums | |
left join fund_products on fund_sums.fund_product_id = fund_products.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stevenharman Thanks for this gist. It helped a lot. What is the magic of this:
Why
.engine
?