0

I have a table that has 3 columns I care about: Name, Id and ParentId. I would like to find out how many children an Id has which will require me to go through each row.

Here's what I have so far:

CREATE TEMPORARY FUNCTION numChildren(givenId INT64)
  RETURNS INT64
  AS ((SELECT count(*) FROM `playground.table` WHERE ParentId = givenId));

SELECT numChildren(Id) OVER() as Test FROM `playground.table`

The exact error I'm getting is: Error: Function USER_DEFINED_FUNCTIONS:NUMCHILDREN does not support an OVER clause at [5:8].

I want to go through each row, pass the Id into the function and then get back the number of rows that have that Id as the ParentId.

Am I going about this the right way? Would I have to use the APIs to do a query like this?

1 Answer 1

2

Just remove OVER() as below

CREATE TEMPORARY FUNCTION numChildren(givenId INT64)
RETURNS INT64
  AS ((SELECT count(*) FROM `playground.table` WHERE ParentId = givenId));

SELECT numChildren(Id) as Test FROM `playground.table`  
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks. When would I need to use OVER() as I thought it's used when you want to execute a query on all rows of a table? Is it because I'm using a function where I am already passing in a field from each row of the table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.