Select from bigquery based on more nested columns











up vote
1
down vote

favorite












I need to filter by more nested data in bigquery and I am only able to filter with my query by one.



Basically i need this:



SELECT item_id FROM table WHERE item_id IS NOT NULL AND page_id = '23784'


Is such thing possible?



I have data in bigquery like below, page_id doesn't have to exist:



| row | date | event      | params.key    | params.value |
-------------------------------------------------------
| 1 | 2018 | screenShow | item_id | 1 |
| page_id | 23784 |
| irrelevant_id | 5 |
| 2 | 2018 | screenShow | item_id | 2 |
| irrelevant_id | 7 |


My query is:



SELECT param.value
FROM `table`,
UNNEST(params) AS param
WHERE
event = 'screenShow'
AND param.key = 'item_id'


but this obviously works only for one key and i don't know how to add the page_id part.
Thanks.










share|improve this question
























  • What do you want returned?
    – Gordon Linoff
    Nov 9 at 13:28










  • All item_ids where item_id is not null and page_id is 23784
    – Ladislav Tomsa
    Nov 9 at 14:26















up vote
1
down vote

favorite












I need to filter by more nested data in bigquery and I am only able to filter with my query by one.



Basically i need this:



SELECT item_id FROM table WHERE item_id IS NOT NULL AND page_id = '23784'


Is such thing possible?



I have data in bigquery like below, page_id doesn't have to exist:



| row | date | event      | params.key    | params.value |
-------------------------------------------------------
| 1 | 2018 | screenShow | item_id | 1 |
| page_id | 23784 |
| irrelevant_id | 5 |
| 2 | 2018 | screenShow | item_id | 2 |
| irrelevant_id | 7 |


My query is:



SELECT param.value
FROM `table`,
UNNEST(params) AS param
WHERE
event = 'screenShow'
AND param.key = 'item_id'


but this obviously works only for one key and i don't know how to add the page_id part.
Thanks.










share|improve this question
























  • What do you want returned?
    – Gordon Linoff
    Nov 9 at 13:28










  • All item_ids where item_id is not null and page_id is 23784
    – Ladislav Tomsa
    Nov 9 at 14:26













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to filter by more nested data in bigquery and I am only able to filter with my query by one.



Basically i need this:



SELECT item_id FROM table WHERE item_id IS NOT NULL AND page_id = '23784'


Is such thing possible?



I have data in bigquery like below, page_id doesn't have to exist:



| row | date | event      | params.key    | params.value |
-------------------------------------------------------
| 1 | 2018 | screenShow | item_id | 1 |
| page_id | 23784 |
| irrelevant_id | 5 |
| 2 | 2018 | screenShow | item_id | 2 |
| irrelevant_id | 7 |


My query is:



SELECT param.value
FROM `table`,
UNNEST(params) AS param
WHERE
event = 'screenShow'
AND param.key = 'item_id'


but this obviously works only for one key and i don't know how to add the page_id part.
Thanks.










share|improve this question















I need to filter by more nested data in bigquery and I am only able to filter with my query by one.



Basically i need this:



SELECT item_id FROM table WHERE item_id IS NOT NULL AND page_id = '23784'


Is such thing possible?



I have data in bigquery like below, page_id doesn't have to exist:



| row | date | event      | params.key    | params.value |
-------------------------------------------------------
| 1 | 2018 | screenShow | item_id | 1 |
| page_id | 23784 |
| irrelevant_id | 5 |
| 2 | 2018 | screenShow | item_id | 2 |
| irrelevant_id | 7 |


My query is:



SELECT param.value
FROM `table`,
UNNEST(params) AS param
WHERE
event = 'screenShow'
AND param.key = 'item_id'


but this obviously works only for one key and i don't know how to add the page_id part.
Thanks.







sql google-bigquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 14:58

























asked Nov 9 at 13:11









Ladislav Tomsa

30116




30116












  • What do you want returned?
    – Gordon Linoff
    Nov 9 at 13:28










  • All item_ids where item_id is not null and page_id is 23784
    – Ladislav Tomsa
    Nov 9 at 14:26


















  • What do you want returned?
    – Gordon Linoff
    Nov 9 at 13:28










  • All item_ids where item_id is not null and page_id is 23784
    – Ladislav Tomsa
    Nov 9 at 14:26
















What do you want returned?
– Gordon Linoff
Nov 9 at 13:28




What do you want returned?
– Gordon Linoff
Nov 9 at 13:28












All item_ids where item_id is not null and page_id is 23784
– Ladislav Tomsa
Nov 9 at 14:26




All item_ids where item_id is not null and page_id is 23784
– Ladislav Tomsa
Nov 9 at 14:26












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted











All item_ids where item_id is not null and page_id is 23784




Below is for BigQuery Standard SQL



#standardSQL
SELECT
(SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
FROM `project.dataset.table`
WHERE (
SELECT COUNT(1)
FROM UNNEST(params) param
WHERE param = ('page_id', 23784)
OR key = 'item_id'
) = 2


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 2018 dt, 'screenShow' event,
[STRUCT<key STRING, value INT64>('item_id', 1), ('page_id', 23784), ('irrelevant_id', 5)] params UNION ALL
SELECT 2018 dt, 'screenShow' event,
[STRUCT<key STRING, value INT64>('item_id', 2), ('irrelevant_id', 7)] params UNION ALL
SELECT 2018 dt, 'screenShow' event,
[STRUCT<key STRING, value INT64>('item_id2', 1), ('page_id', 23784), ('irrelevant_id', 5)] params
)
SELECT
(SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
FROM `project.dataset.table`
WHERE (
SELECT COUNT(1)
FROM UNNEST(params) param
WHERE param = ('page_id', 23784)
OR key = 'item_id'
) = 2


with result



Row item_id  
1 1


Obviously, if instead just list of item_id's you need whole row - you just use SELECT * as in below



#standardSQL
SELECT *
FROM `project.dataset.table`
WHERE (
SELECT COUNT(1)
FROM UNNEST(params) param
WHERE param = ('page_id', 23784)
OR key = 'item_id'
) = 2


in this case you will get



| row | date | event      | params.key    | params.value |
-------------------------------------------------------
| 1 | 2018 | screenShow | item_id | 1 |
| page_id | 23784 |
| irrelevant_id | 5 |





share|improve this answer




























    up vote
    0
    down vote













    Try the following:



    SELECT
    (SELECT x.value FROM UNNEST(params) AS x WHERE x.key = 'item_id') AS item_id
    FROM
    `your_dataset.your_table`
    WHERE
    EXISTS (
    SELECT
    *
    FROM
    UNNEST(params) AS x
    JOIN
    UNNEST (params) AS y
    WHERE
    x.key = 'item_id'
    AND x.value IS NOT NULL
    AND y.key = 'page_id'
    AND y.value=23784)





    share|improve this answer




























      up vote
      0
      down vote













      Well, you could do:



      select t.*
      from t
      where exists (select 1 from unnest(params) p where p.key = 'item_id' and p.value is not null) and
      exists (select 1 from unnest(params) p where p.key = 'page_id' and p.value = 23784);





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226352%2fselect-from-bigquery-based-on-more-nested-columns%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted











        All item_ids where item_id is not null and page_id is 23784




        Below is for BigQuery Standard SQL



        #standardSQL
        SELECT
        (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
        FROM `project.dataset.table`
        WHERE (
        SELECT COUNT(1)
        FROM UNNEST(params) param
        WHERE param = ('page_id', 23784)
        OR key = 'item_id'
        ) = 2


        You can test, play with above using dummy data as below



        #standardSQL
        WITH `project.dataset.table` AS (
        SELECT 2018 dt, 'screenShow' event,
        [STRUCT<key STRING, value INT64>('item_id', 1), ('page_id', 23784), ('irrelevant_id', 5)] params UNION ALL
        SELECT 2018 dt, 'screenShow' event,
        [STRUCT<key STRING, value INT64>('item_id', 2), ('irrelevant_id', 7)] params UNION ALL
        SELECT 2018 dt, 'screenShow' event,
        [STRUCT<key STRING, value INT64>('item_id2', 1), ('page_id', 23784), ('irrelevant_id', 5)] params
        )
        SELECT
        (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
        FROM `project.dataset.table`
        WHERE (
        SELECT COUNT(1)
        FROM UNNEST(params) param
        WHERE param = ('page_id', 23784)
        OR key = 'item_id'
        ) = 2


        with result



        Row item_id  
        1 1


        Obviously, if instead just list of item_id's you need whole row - you just use SELECT * as in below



        #standardSQL
        SELECT *
        FROM `project.dataset.table`
        WHERE (
        SELECT COUNT(1)
        FROM UNNEST(params) param
        WHERE param = ('page_id', 23784)
        OR key = 'item_id'
        ) = 2


        in this case you will get



        | row | date | event      | params.key    | params.value |
        -------------------------------------------------------
        | 1 | 2018 | screenShow | item_id | 1 |
        | page_id | 23784 |
        | irrelevant_id | 5 |





        share|improve this answer

























          up vote
          1
          down vote



          accepted











          All item_ids where item_id is not null and page_id is 23784




          Below is for BigQuery Standard SQL



          #standardSQL
          SELECT
          (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
          FROM `project.dataset.table`
          WHERE (
          SELECT COUNT(1)
          FROM UNNEST(params) param
          WHERE param = ('page_id', 23784)
          OR key = 'item_id'
          ) = 2


          You can test, play with above using dummy data as below



          #standardSQL
          WITH `project.dataset.table` AS (
          SELECT 2018 dt, 'screenShow' event,
          [STRUCT<key STRING, value INT64>('item_id', 1), ('page_id', 23784), ('irrelevant_id', 5)] params UNION ALL
          SELECT 2018 dt, 'screenShow' event,
          [STRUCT<key STRING, value INT64>('item_id', 2), ('irrelevant_id', 7)] params UNION ALL
          SELECT 2018 dt, 'screenShow' event,
          [STRUCT<key STRING, value INT64>('item_id2', 1), ('page_id', 23784), ('irrelevant_id', 5)] params
          )
          SELECT
          (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
          FROM `project.dataset.table`
          WHERE (
          SELECT COUNT(1)
          FROM UNNEST(params) param
          WHERE param = ('page_id', 23784)
          OR key = 'item_id'
          ) = 2


          with result



          Row item_id  
          1 1


          Obviously, if instead just list of item_id's you need whole row - you just use SELECT * as in below



          #standardSQL
          SELECT *
          FROM `project.dataset.table`
          WHERE (
          SELECT COUNT(1)
          FROM UNNEST(params) param
          WHERE param = ('page_id', 23784)
          OR key = 'item_id'
          ) = 2


          in this case you will get



          | row | date | event      | params.key    | params.value |
          -------------------------------------------------------
          | 1 | 2018 | screenShow | item_id | 1 |
          | page_id | 23784 |
          | irrelevant_id | 5 |





          share|improve this answer























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted







            All item_ids where item_id is not null and page_id is 23784




            Below is for BigQuery Standard SQL



            #standardSQL
            SELECT
            (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            You can test, play with above using dummy data as below



            #standardSQL
            WITH `project.dataset.table` AS (
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id', 1), ('page_id', 23784), ('irrelevant_id', 5)] params UNION ALL
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id', 2), ('irrelevant_id', 7)] params UNION ALL
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id2', 1), ('page_id', 23784), ('irrelevant_id', 5)] params
            )
            SELECT
            (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            with result



            Row item_id  
            1 1


            Obviously, if instead just list of item_id's you need whole row - you just use SELECT * as in below



            #standardSQL
            SELECT *
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            in this case you will get



            | row | date | event      | params.key    | params.value |
            -------------------------------------------------------
            | 1 | 2018 | screenShow | item_id | 1 |
            | page_id | 23784 |
            | irrelevant_id | 5 |





            share|improve this answer













            All item_ids where item_id is not null and page_id is 23784




            Below is for BigQuery Standard SQL



            #standardSQL
            SELECT
            (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            You can test, play with above using dummy data as below



            #standardSQL
            WITH `project.dataset.table` AS (
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id', 1), ('page_id', 23784), ('irrelevant_id', 5)] params UNION ALL
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id', 2), ('irrelevant_id', 7)] params UNION ALL
            SELECT 2018 dt, 'screenShow' event,
            [STRUCT<key STRING, value INT64>('item_id2', 1), ('page_id', 23784), ('irrelevant_id', 5)] params
            )
            SELECT
            (SELECT value FROM UNNEST(params) param WHERE key = 'item_id') item_id
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            with result



            Row item_id  
            1 1


            Obviously, if instead just list of item_id's you need whole row - you just use SELECT * as in below



            #standardSQL
            SELECT *
            FROM `project.dataset.table`
            WHERE (
            SELECT COUNT(1)
            FROM UNNEST(params) param
            WHERE param = ('page_id', 23784)
            OR key = 'item_id'
            ) = 2


            in this case you will get



            | row | date | event      | params.key    | params.value |
            -------------------------------------------------------
            | 1 | 2018 | screenShow | item_id | 1 |
            | page_id | 23784 |
            | irrelevant_id | 5 |






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 18:40









            Mikhail Berlyant

            54.7k43368




            54.7k43368
























                up vote
                0
                down vote













                Try the following:



                SELECT
                (SELECT x.value FROM UNNEST(params) AS x WHERE x.key = 'item_id') AS item_id
                FROM
                `your_dataset.your_table`
                WHERE
                EXISTS (
                SELECT
                *
                FROM
                UNNEST(params) AS x
                JOIN
                UNNEST (params) AS y
                WHERE
                x.key = 'item_id'
                AND x.value IS NOT NULL
                AND y.key = 'page_id'
                AND y.value=23784)





                share|improve this answer

























                  up vote
                  0
                  down vote













                  Try the following:



                  SELECT
                  (SELECT x.value FROM UNNEST(params) AS x WHERE x.key = 'item_id') AS item_id
                  FROM
                  `your_dataset.your_table`
                  WHERE
                  EXISTS (
                  SELECT
                  *
                  FROM
                  UNNEST(params) AS x
                  JOIN
                  UNNEST (params) AS y
                  WHERE
                  x.key = 'item_id'
                  AND x.value IS NOT NULL
                  AND y.key = 'page_id'
                  AND y.value=23784)





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Try the following:



                    SELECT
                    (SELECT x.value FROM UNNEST(params) AS x WHERE x.key = 'item_id') AS item_id
                    FROM
                    `your_dataset.your_table`
                    WHERE
                    EXISTS (
                    SELECT
                    *
                    FROM
                    UNNEST(params) AS x
                    JOIN
                    UNNEST (params) AS y
                    WHERE
                    x.key = 'item_id'
                    AND x.value IS NOT NULL
                    AND y.key = 'page_id'
                    AND y.value=23784)





                    share|improve this answer












                    Try the following:



                    SELECT
                    (SELECT x.value FROM UNNEST(params) AS x WHERE x.key = 'item_id') AS item_id
                    FROM
                    `your_dataset.your_table`
                    WHERE
                    EXISTS (
                    SELECT
                    *
                    FROM
                    UNNEST(params) AS x
                    JOIN
                    UNNEST (params) AS y
                    WHERE
                    x.key = 'item_id'
                    AND x.value IS NOT NULL
                    AND y.key = 'page_id'
                    AND y.value=23784)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 15:15









                    F10

                    1,1911414




                    1,1911414






















                        up vote
                        0
                        down vote













                        Well, you could do:



                        select t.*
                        from t
                        where exists (select 1 from unnest(params) p where p.key = 'item_id' and p.value is not null) and
                        exists (select 1 from unnest(params) p where p.key = 'page_id' and p.value = 23784);





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Well, you could do:



                          select t.*
                          from t
                          where exists (select 1 from unnest(params) p where p.key = 'item_id' and p.value is not null) and
                          exists (select 1 from unnest(params) p where p.key = 'page_id' and p.value = 23784);





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Well, you could do:



                            select t.*
                            from t
                            where exists (select 1 from unnest(params) p where p.key = 'item_id' and p.value is not null) and
                            exists (select 1 from unnest(params) p where p.key = 'page_id' and p.value = 23784);





                            share|improve this answer












                            Well, you could do:



                            select t.*
                            from t
                            where exists (select 1 from unnest(params) p where p.key = 'item_id' and p.value is not null) and
                            exists (select 1 from unnest(params) p where p.key = 'page_id' and p.value = 23784);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 16:09









                            Gordon Linoff

                            752k34286394




                            752k34286394






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226352%2fselect-from-bigquery-based-on-more-nested-columns%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()