2017-09-26 122 views
0

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

这里是我的数据集:

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 

回答

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))) 
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; 
+0

这样的作品,但有没有办法做到这一点使用'IN'运营商作为我查询了很多比样本数据更复杂设置我张贴并依靠'children_ids' – Coderama

+0

@Coderama。 。 。你不能像这样使用'IN'。你可以聚合成一个数组并使用数组函数。 –

相关问题