how to pass null = null in the where clause












0















I have a bigquery select statement where in some cases, the where clauses show null = null



In the case of this statement:



select * from `db.ds.table1` t1
join `db.ds.table2` t2
on t1.first_name = t2.first_name
and t1.last_name = t2.last_name


as you can see, this would appear to be looking for people, however, in some cases, I have businesses in the tables. In those cases, first_name is used, and last_name is null.



this means I have a null = null scenerio, and fails join.



How do I tell bigquery to allow null = null, without having to put coalesce statements all over the place?



Thanks










share|improve this question



























    0















    I have a bigquery select statement where in some cases, the where clauses show null = null



    In the case of this statement:



    select * from `db.ds.table1` t1
    join `db.ds.table2` t2
    on t1.first_name = t2.first_name
    and t1.last_name = t2.last_name


    as you can see, this would appear to be looking for people, however, in some cases, I have businesses in the tables. In those cases, first_name is used, and last_name is null.



    this means I have a null = null scenerio, and fails join.



    How do I tell bigquery to allow null = null, without having to put coalesce statements all over the place?



    Thanks










    share|improve this question

























      0












      0








      0








      I have a bigquery select statement where in some cases, the where clauses show null = null



      In the case of this statement:



      select * from `db.ds.table1` t1
      join `db.ds.table2` t2
      on t1.first_name = t2.first_name
      and t1.last_name = t2.last_name


      as you can see, this would appear to be looking for people, however, in some cases, I have businesses in the tables. In those cases, first_name is used, and last_name is null.



      this means I have a null = null scenerio, and fails join.



      How do I tell bigquery to allow null = null, without having to put coalesce statements all over the place?



      Thanks










      share|improve this question














      I have a bigquery select statement where in some cases, the where clauses show null = null



      In the case of this statement:



      select * from `db.ds.table1` t1
      join `db.ds.table2` t2
      on t1.first_name = t2.first_name
      and t1.last_name = t2.last_name


      as you can see, this would appear to be looking for people, however, in some cases, I have businesses in the tables. In those cases, first_name is used, and last_name is null.



      this means I have a null = null scenerio, and fails join.



      How do I tell bigquery to allow null = null, without having to put coalesce statements all over the place?



      Thanks







      sql google-bigquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 4:04









      arcee123arcee123

      75611536




      75611536
























          2 Answers
          2






          active

          oldest

          votes


















          2














          #standardSQL
          select * from `db.ds.table1` t1
          join `db.ds.table2` t2
          on t1.first_name = t2.first_name
          and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')


          obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason






          share|improve this answer


























          • This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

            – Tim Biegeleisen
            Nov 25 '18 at 13:36






          • 1





            hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

            – Mikhail Berlyant
            Nov 25 '18 at 15:18











          • well, I didn't want to have to put that on every field, but I guess there's no way around that.

            – arcee123
            Nov 29 '18 at 3:12











          • Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

            – Mikhail Berlyant
            Nov 29 '18 at 3:58



















          3














          Make the last name optional, and allow both last names being NULL as valid:



          SELECT *
          FROM `db.ds.table1` t1
          INNER JOIN `db.ds.table2` t2
          ON t1.first_name = t2.first_name AND
          (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));


          We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.






          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',
            autoActivateHeartbeat: false,
            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%2f53464556%2fhow-to-pass-null-null-in-the-where-clause%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            #standardSQL
            select * from `db.ds.table1` t1
            join `db.ds.table2` t2
            on t1.first_name = t2.first_name
            and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')


            obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason






            share|improve this answer


























            • This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

              – Tim Biegeleisen
              Nov 25 '18 at 13:36






            • 1





              hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

              – Mikhail Berlyant
              Nov 25 '18 at 15:18











            • well, I didn't want to have to put that on every field, but I guess there's no way around that.

              – arcee123
              Nov 29 '18 at 3:12











            • Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

              – Mikhail Berlyant
              Nov 29 '18 at 3:58
















            2














            #standardSQL
            select * from `db.ds.table1` t1
            join `db.ds.table2` t2
            on t1.first_name = t2.first_name
            and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')


            obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason






            share|improve this answer


























            • This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

              – Tim Biegeleisen
              Nov 25 '18 at 13:36






            • 1





              hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

              – Mikhail Berlyant
              Nov 25 '18 at 15:18











            • well, I didn't want to have to put that on every field, but I guess there's no way around that.

              – arcee123
              Nov 29 '18 at 3:12











            • Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

              – Mikhail Berlyant
              Nov 29 '18 at 3:58














            2












            2








            2







            #standardSQL
            select * from `db.ds.table1` t1
            join `db.ds.table2` t2
            on t1.first_name = t2.first_name
            and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')


            obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason






            share|improve this answer















            #standardSQL
            select * from `db.ds.table1` t1
            join `db.ds.table2` t2
            on t1.first_name = t2.first_name
            and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')


            obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 4:45

























            answered Nov 25 '18 at 4:33









            Mikhail BerlyantMikhail Berlyant

            57.4k43571




            57.4k43571













            • This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

              – Tim Biegeleisen
              Nov 25 '18 at 13:36






            • 1





              hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

              – Mikhail Berlyant
              Nov 25 '18 at 15:18











            • well, I didn't want to have to put that on every field, but I guess there's no way around that.

              – arcee123
              Nov 29 '18 at 3:12











            • Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

              – Mikhail Berlyant
              Nov 29 '18 at 3:58



















            • This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

              – Tim Biegeleisen
              Nov 25 '18 at 13:36






            • 1





              hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

              – Mikhail Berlyant
              Nov 25 '18 at 15:18











            • well, I didn't want to have to put that on every field, but I guess there's no way around that.

              – arcee123
              Nov 29 '18 at 3:12











            • Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

              – Mikhail Berlyant
              Nov 29 '18 at 3:58

















            This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

            – Tim Biegeleisen
            Nov 25 '18 at 13:36





            This might be OK, but in the (possibly rare) case that empty string should be recorded as either last_name, this query could give false results.

            – Tim Biegeleisen
            Nov 25 '18 at 13:36




            1




            1





            hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

            – Mikhail Berlyant
            Nov 25 '18 at 15:18





            hope OP will be able to come with the appropriate value if '' will conflict with possible existing values for last name - if such edge cases exist

            – Mikhail Berlyant
            Nov 25 '18 at 15:18













            well, I didn't want to have to put that on every field, but I guess there's no way around that.

            – arcee123
            Nov 29 '18 at 3:12





            well, I didn't want to have to put that on every field, but I guess there's no way around that.

            – arcee123
            Nov 29 '18 at 3:12













            Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

            – Mikhail Berlyant
            Nov 29 '18 at 3:58





            Just to clarify - Do you mean in your real use case you have multiple such fields like last name?

            – Mikhail Berlyant
            Nov 29 '18 at 3:58













            3














            Make the last name optional, and allow both last names being NULL as valid:



            SELECT *
            FROM `db.ds.table1` t1
            INNER JOIN `db.ds.table2` t2
            ON t1.first_name = t2.first_name AND
            (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));


            We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.






            share|improve this answer




























              3














              Make the last name optional, and allow both last names being NULL as valid:



              SELECT *
              FROM `db.ds.table1` t1
              INNER JOIN `db.ds.table2` t2
              ON t1.first_name = t2.first_name AND
              (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));


              We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.






              share|improve this answer


























                3












                3








                3







                Make the last name optional, and allow both last names being NULL as valid:



                SELECT *
                FROM `db.ds.table1` t1
                INNER JOIN `db.ds.table2` t2
                ON t1.first_name = t2.first_name AND
                (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));


                We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.






                share|improve this answer













                Make the last name optional, and allow both last names being NULL as valid:



                SELECT *
                FROM `db.ds.table1` t1
                INNER JOIN `db.ds.table2` t2
                ON t1.first_name = t2.first_name AND
                (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));


                We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 4:09









                Tim BiegeleisenTim Biegeleisen

                222k1389142




                222k1389142






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464556%2fhow-to-pass-null-null-in-the-where-clause%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







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks