beanquery has some very limited support for subqueries. In particular,
it does not support subqueries that are not constants. However, it
didn't had support for subqueries on the right hand side of the IN
operator. Adding minimal support for it is relatively easy, so I did it,
see
https://github.com/beancount/beanquery/pull/208. Expanding the
subquery support is in the works.
However, I guess that your attempt failed at an earlier stage of the
query interpretation: the subquery is invalid.
SELECT account WHERE abs(sum(position)) > 50
Because of the use of sum() this is aggregate query (the fact is
disguised by the use of the BQL auto-aggregate functionality) and it is
trying to filter the rows by the value of the aggregated column. This is
not a valid operation in SQL. The query would have to be rewritten in
this way:
SELECT account GROUP BY account HAVING abs(sum(position)) > 50
However, the condition for the HAVING clause is incorrect: sum(position)
returns an inventory, and the abs() of an inventory is still an
inventory, which cannot be compared with a number. You would need to
extract the amount of a single currency in the inventory, and get the
numerical part of the amount and discard the currency. I also thing the
use of abs() is not useful, as you are interested only in donors, thus
the inventory should always be positive. Therefore, the subquery becomes:
SELECT account
GROUP BY account
HAVING number(only('USD', sum(position))) > 50.0
However, this is computing a lot of inventories that are not really
useful for the outer query and making the set to be check by the IN
operator unnecessarily large. Adding a WHERE clause helps:
SELECT account
WHERE root(account, 2) = 'Income:Contributions'
GROUP BY account
HAVING number(only('USD', sum(position))) > 50.0
and you can drop the equivalent condition from the outer query.
Cheers,
Dan