Subquery in select statement?
Hi, I'm trying to grab a value from a subquery in my select statement for a computed column but I'm getting an error telling me the column from the subquery doesn't exist.
Attempting to use it inside of the select, here:
Solution:Jump to solution
👋 you can use a plain query as sub select: https://drizzle.run/yxbxn6x4rloxney2nvc1tphp
Drizzle Run
Select with sub query - Drizzle Run
5 Replies
FYI this works if I execute the issuances query first and just use the value from the row that gets returned.
Solution
👋 you can use a plain query as sub select: https://drizzle.run/yxbxn6x4rloxney2nvc1tphp
Drizzle Run
Select with sub query - Drizzle Run
That worked! Thank you!
Ah, question though. Does this query happen on each row that gets returned or just once and the result is "cached"?
Good question. The db engine is capable of optimizing sub queries in from and cte (queries with “with”).
In this playground I don’t think it is cached and it happens to every row since it is called in a select.
Appreciate the help, thank you!
So what I ended up doing was doing a .leftJoin(subquery, sql'true') instead of putting it into select but would it be even better to put it in a CTE? What would the difference be if any?