0

我有一列children_ids包含来自 STRING_AGG 函数的 PK。我正在尝试在WHERE带有运算符的子句中使用此列IN来返回,total_pets但它不起作用。如果我将值直接复制并粘贴到IN运算符中,则查询将返回正确的信息,否则找不到任何结果。

这是我的数据集:

Parents
=======
id  parent_name
----------------
1   Bob and Mary
2   Mick and Jo

Children
========
id  child_name  parent_id
-------------------------
1   Eddie       1
2   Frankie     1
3   Robbie      1
4   Duncan      2
5   Rick        2
6   Jen         2

Childrens Pets
===============
id  pet_name  child_id
-------------------------
1   Puppy     1
2   Piggy     2
3   Monkey    3
4   Lamb      4
5   Tiger     5
6   Bear      6
7   Zebra     6

Expected Output
===============
parent_id  children_ids  total_pets
-----------------------------------
1          1,2,3         3
2          4,5,6         4

Current [undesired] Output
==========================
parent_id  children_ids  total_pets
-----------------------------------
1          1,2,3         0
2          4,5,6         0

这是您自己测试的标准sql

# setup data with standardSQL
WITH `parents` AS (
  SELECT 1 id, 'Bob and Mary' parent_names UNION ALL
  SELECT 2, 'Mick and Jo'
),
`children` AS (
  SELECT 1 id, 'Eddie' child_name, 1 parent_id UNION ALL     
  SELECT 2, 'Frankie', 1 UNION ALL     
  SELECT 3, 'Robbie', 1 UNION ALL     
  SELECT 4, 'Duncan', 2 UNION ALL
  SELECT 5, 'Rick', 2 UNION ALL
  SELECT 6, 'Jen', 2
),
`childrens_pets` AS (
  SELECT 1 id, 'Puppy' pet_name, 1 child_id UNION ALL     
  SELECT 2, 'Piggy', 2 UNION ALL     
  SELECT 3, 'Monkey', 3 UNION ALL     
  SELECT 4, 'Lamb', 4 UNION ALL
  SELECT 5, 'Tiger', 5 UNION ALL
  SELECT 6, 'Bear', 6 UNION ALL 
  SELECT 7, 'Zebra', 6
)

和查询:

#standardSQL
select
  parent_id
  , children_ids

-- !!! This keeps returning 0 instead of the total pets for each parent based on their children
  , (
    select count(p1.id)
    from childrens_pets p1
    where cast(p1.child_id as string) in (children_ids)
  ) as total_pets
from
(
  SELECT 
    p.id as parent_id
    , (
      select string_agg(cast(c1.id as string))
      from children as c1
      where c1.parent_id = p.id
    ) as children_ids

  FROM parents as p
    join children as c
      on p.id = c.parent_id
    join childrens_pets as cp
      on cp.child_id = c.id
)
GROUP BY
  parent_id
  , children_ids 
4

2 回答 2

2

...但是有没有办法使用 IN 运算符作为我的查询来做到这一点...

只需修复一条线,它就会为您工作!

代替

WHERE CAST(p1.child_id AS STRING) IN (children_ids)   

WHERE CAST(p1.child_id AS STRING) IN (SELECT * FROM UNNEST(SPLIT(children_ids)))
于 2017-09-26T15:46:27.053 回答
1

嗯?这似乎可以满足您的要求:

SELECT p.id as parent_id,
       string_agg(distinct cast(c.id as string)) as children_ids
       count(distinct cp.id) as num_pets
FROM parents p JOIN
     children c
     ON p.id = c.parent_id JOIN
     children_pets cp
     ON cp.child_id = c.id
GROUP BY parent_id;
于 2017-09-26T12:08:33.313 回答