BigQuery를 사용하여 Arry 간의 관계 비교

24267 단어 GCPSQLBigQuerytech

비교할 배열


with items as (
              select 1 as id, ['dog'] as tags
    union all select 2 as id, ['cat'] as tags
    union all select 3 as id, ['bird'] as tags
    union all select 4 as id, ['dog', 'cat'] as tags
    union all select 5 as id, ['cat', 'bird'] as tags
    union all select 6 as id, ['bird', 'dog'] as tags
    union all select 7 as id, ['dog', 'cat', 'bird'] as tags
)
select
    *
from
    items;
id
tags
1
[dog]
2
[cat]
3
[bird]
4
[dog,cat]
5
[cat,bird]
6
[bird,dog]
7
[dog,cat,bird]
이 책상이랑이러한 배열은 각종 조건하에서 비교된 상황에서의 조회이다.
만약 조건이 같고 여러 가지 문법이 있다면 생각한 부분만 나열해 보세요.

비교 조건


객체 패턴을 비교하는 요소는 해당 패턴에서 고유하다고 가정합니다.
중복이 있는 경우 중복을 제거하기 위한 사전 처리가 진행된 상태에서 비교한다고 가정합니다.

모든 배열의 요소


id
tags
4
[dog,cat]
7
[dog,cat,bird]
select
    *
from
    items
where
    (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    ) = array_length(['dog', 'cat']);

배열의 요소가 동일함


id
tags
4
[dog,cat]
select
    *
from
    items
where
    array_length(tags) = array_length(['dog', 'cat'])
    and (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    ) = array_length(['dog', 'cat']);

배열에 하나의 요소만 있음


id
tags
1
[dog]
2
[cat]
5
[cat,bird]
6
[bird,dog]
select
    *
from
    items
where
    (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    ) = 1;

배열에 하나 이상의 요소가 있습니다.


id
tags
1
[dog]
2
[cat]
4
[dog,cat]
5
[cat,bird]
6
[bird,dog]
7
[dog,cat,bird]
select
    *
from
    items
where
    (
        select
            logical_or(t in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not (
        select
            logical_and(t not in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    exists (
        select
            *
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    );
select
    *
from
    items
where
    (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    ) > 0;

배열의 요소로만 구성


id
tags
1
[dog]
2
[cat]
4
[dog,cat]
select
    *
from
    items
where
    (
        select
            logical_and(t in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not (
        select
            logical_or(t not in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not exists (
        select
            *
        from
            unnest(tags) as t
        where
            t not in ('dog', 'cat')
    );
select
    *
from
    items
where
    (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t not in ('dog', 'cat')
    ) = 0;

배열에 요소가 없습니다.


id
tags
3
[bird]
select
    *
from
    items
where
    (
        select
            logical_and(t not in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not (
        select
            logical_or(t in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not exists (
        select
            *
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    );
select
    *
from
    items
where
    (
        select
            count(1)
        from
            unnest(tags) as t
        where
            t in ('dog', 'cat')
    ) = 0;

하나 이상의 그룹에 없는 원소


id
tags
3
[bird]
5
[cat,bird]
6
[bird,dog]
7
[dog,cat,bird]
select
    *
from
    items
where
    (
        select
            logical_or(t not in ('dog', 'cat'))
        from
            unnest(tags) as t
    );
select
    *
from
    items
where
    not (
        select
            logical_and(t in ('dog', 'cat'))
        from
            unnest(tags) as t
    );

좋은 웹페이지 즐겨찾기