I'm trying to select all parents with the count of their children.
I have the following query:
SELECT
a.*,
(SELECT COUNT(*) FROM demo b WHERE b.parent = a.name) as count
FROM demo a
WHERE
meta(a).id LIKE "xyz:%"
AND a.parent IS MISSING
ORDER BY a.createdAt DESC LIMIT 50 OFFSET 0
My documents look similar to:
xyz:1
{
id: 1,
name: "parent",
createdAt: 1234
}
xyz:2
{
id: 2,
name: "child",
parent: "parent",
createdAt: 5678
}
I get the below error:
Error evaluating projection. - cause: FROM in correlated subquery must have USE KEYS clause: FROM demo.
Error code: 5010
UPDATE: The below query seem to work:
SELECT
a.*,
(SELECT COUNT(id) as count FROM demo b WHERE b.parent = "parent")[0].count as count
FROM demo a
WHERE
meta(a).id LIKE "xyz:%"
AND a.parent IS MISSING
ORDER BY a.createdAt DESC LIMIT 50 OFFSET 0
but if I replace "parent" with a.name it gives the same error.