New column referencing second table - do I need a join?












1















I have two tables (first two shown) and need to make a third from the first two - do I need to do a join or can you reference a table without joining?



The third table shown is the desired output. Thanks for any help!



| ACC | CALL DATE |  |  |
+-----+-----------+--+--+
| 1 1 | 2/1/18 | | |
+-----+-----------+--

+-----+---------------+--+--+
| ACC | PURCHASE DATE | | |
+-----+---------------+--+--+
| 1 1 | 1/1/18 | | |
+-----+---------------+--+--+

+-----+-----------+----------------------+--+
| ACC | CALL DATE | PRIOR MONTH PURCHASE | |
+-----+-----------+----------------------+--+
| 1 1 | 2/1/18 | YES | |
+-----+-----------+----------------------+--+









share|improve this question

























  • MySQL <> SQL Server. Please specify which RDBMS are you using ?

    – Madhur Bhaiya
    Nov 18 '18 at 4:34






  • 1





    That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

    – Sami Kuhmonen
    Nov 18 '18 at 4:44






  • 1





    Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

    – quant
    Nov 18 '18 at 7:22











  • got it thank you

    – newbie
    Nov 18 '18 at 18:32
















1















I have two tables (first two shown) and need to make a third from the first two - do I need to do a join or can you reference a table without joining?



The third table shown is the desired output. Thanks for any help!



| ACC | CALL DATE |  |  |
+-----+-----------+--+--+
| 1 1 | 2/1/18 | | |
+-----+-----------+--

+-----+---------------+--+--+
| ACC | PURCHASE DATE | | |
+-----+---------------+--+--+
| 1 1 | 1/1/18 | | |
+-----+---------------+--+--+

+-----+-----------+----------------------+--+
| ACC | CALL DATE | PRIOR MONTH PURCHASE | |
+-----+-----------+----------------------+--+
| 1 1 | 2/1/18 | YES | |
+-----+-----------+----------------------+--+









share|improve this question

























  • MySQL <> SQL Server. Please specify which RDBMS are you using ?

    – Madhur Bhaiya
    Nov 18 '18 at 4:34






  • 1





    That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

    – Sami Kuhmonen
    Nov 18 '18 at 4:44






  • 1





    Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

    – quant
    Nov 18 '18 at 7:22











  • got it thank you

    – newbie
    Nov 18 '18 at 18:32














1












1








1








I have two tables (first two shown) and need to make a third from the first two - do I need to do a join or can you reference a table without joining?



The third table shown is the desired output. Thanks for any help!



| ACC | CALL DATE |  |  |
+-----+-----------+--+--+
| 1 1 | 2/1/18 | | |
+-----+-----------+--

+-----+---------------+--+--+
| ACC | PURCHASE DATE | | |
+-----+---------------+--+--+
| 1 1 | 1/1/18 | | |
+-----+---------------+--+--+

+-----+-----------+----------------------+--+
| ACC | CALL DATE | PRIOR MONTH PURCHASE | |
+-----+-----------+----------------------+--+
| 1 1 | 2/1/18 | YES | |
+-----+-----------+----------------------+--+









share|improve this question
















I have two tables (first two shown) and need to make a third from the first two - do I need to do a join or can you reference a table without joining?



The third table shown is the desired output. Thanks for any help!



| ACC | CALL DATE |  |  |
+-----+-----------+--+--+
| 1 1 | 2/1/18 | | |
+-----+-----------+--

+-----+---------------+--+--+
| ACC | PURCHASE DATE | | |
+-----+---------------+--+--+
| 1 1 | 1/1/18 | | |
+-----+---------------+--+--+

+-----+-----------+----------------------+--+
| ACC | CALL DATE | PRIOR MONTH PURCHASE | |
+-----+-----------+----------------------+--+
| 1 1 | 2/1/18 | YES | |
+-----+-----------+----------------------+--+






sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 4:34









Madhur Bhaiya

19.6k62236




19.6k62236










asked Nov 18 '18 at 4:34









newbienewbie

175




175













  • MySQL <> SQL Server. Please specify which RDBMS are you using ?

    – Madhur Bhaiya
    Nov 18 '18 at 4:34






  • 1





    That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

    – Sami Kuhmonen
    Nov 18 '18 at 4:44






  • 1





    Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

    – quant
    Nov 18 '18 at 7:22











  • got it thank you

    – newbie
    Nov 18 '18 at 18:32



















  • MySQL <> SQL Server. Please specify which RDBMS are you using ?

    – Madhur Bhaiya
    Nov 18 '18 at 4:34






  • 1





    That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

    – Sami Kuhmonen
    Nov 18 '18 at 4:44






  • 1





    Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

    – quant
    Nov 18 '18 at 7:22











  • got it thank you

    – newbie
    Nov 18 '18 at 18:32

















MySQL <> SQL Server. Please specify which RDBMS are you using ?

– Madhur Bhaiya
Nov 18 '18 at 4:34





MySQL <> SQL Server. Please specify which RDBMS are you using ?

– Madhur Bhaiya
Nov 18 '18 at 4:34




1




1





That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

– Sami Kuhmonen
Nov 18 '18 at 4:44





That depends on what you’re doing. If you need data from several tables you join. If you don’t you don’t join.

– Sami Kuhmonen
Nov 18 '18 at 4:44




1




1





Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

– quant
Nov 18 '18 at 7:22





Hi and welcome to SO. Please edit the question to show what you've tried. Like this we will be able to better understand your problem/question and thus we will be able to better help you. It is best to provide a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the Tour.

– quant
Nov 18 '18 at 7:22













got it thank you

– newbie
Nov 18 '18 at 18:32





got it thank you

– newbie
Nov 18 '18 at 18:32












2 Answers
2






active

oldest

votes


















1














Of course you can have a query that references multiple tables without joining. union all is an example of an operator that does that.



There is also the question of what you mean by "joining" in the question. If you mean explicit joins, there are ways around that -- such as correlated subqueries. However, these are implementing some form of "join" in the database engine.



As for your query, you would want to use exists with a correlated subquery:



select t1.*,
(case when exists (select 1
from table2 t2
where t2.acc = t1.acc and
datediff(month, t2.purchase_date, t1.call_date) = 1
)
then 'Yes' else 'No'
end) as prior_month_purchase
from table1 t1;


This is "better" than a join because it does not multiply or remove rows. The result set has exactly the rows in the first table, with the additional column.



The syntax assumes SQL Server (which was an original tag). Similar logic can be expressed in other databases, although date functions are notoriously database-dependent.






share|improve this answer































    1














    Lets check the options,



    Say if you were to create a new third table on the basis of the data in first two, then every update/inserts/deletes to either of the tables should also propagate into the third table as well.



    Say you instead have a view which does what you need, there isnt a need to maintain that third table and also gets you the data needed from the first two each time you query it.



    create view third_table as
    select a.acc,a.call_date,case when dateadd(mm,-1,a.call_date)=b.purchase_date then 'Yes' else 'No end as prior_month_purchase
    from first_table a
    left join second_table b
    on a.acc=b.acc





    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%2f53357925%2fnew-column-referencing-second-table-do-i-need-a-join%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









      1














      Of course you can have a query that references multiple tables without joining. union all is an example of an operator that does that.



      There is also the question of what you mean by "joining" in the question. If you mean explicit joins, there are ways around that -- such as correlated subqueries. However, these are implementing some form of "join" in the database engine.



      As for your query, you would want to use exists with a correlated subquery:



      select t1.*,
      (case when exists (select 1
      from table2 t2
      where t2.acc = t1.acc and
      datediff(month, t2.purchase_date, t1.call_date) = 1
      )
      then 'Yes' else 'No'
      end) as prior_month_purchase
      from table1 t1;


      This is "better" than a join because it does not multiply or remove rows. The result set has exactly the rows in the first table, with the additional column.



      The syntax assumes SQL Server (which was an original tag). Similar logic can be expressed in other databases, although date functions are notoriously database-dependent.






      share|improve this answer




























        1














        Of course you can have a query that references multiple tables without joining. union all is an example of an operator that does that.



        There is also the question of what you mean by "joining" in the question. If you mean explicit joins, there are ways around that -- such as correlated subqueries. However, these are implementing some form of "join" in the database engine.



        As for your query, you would want to use exists with a correlated subquery:



        select t1.*,
        (case when exists (select 1
        from table2 t2
        where t2.acc = t1.acc and
        datediff(month, t2.purchase_date, t1.call_date) = 1
        )
        then 'Yes' else 'No'
        end) as prior_month_purchase
        from table1 t1;


        This is "better" than a join because it does not multiply or remove rows. The result set has exactly the rows in the first table, with the additional column.



        The syntax assumes SQL Server (which was an original tag). Similar logic can be expressed in other databases, although date functions are notoriously database-dependent.






        share|improve this answer


























          1












          1








          1







          Of course you can have a query that references multiple tables without joining. union all is an example of an operator that does that.



          There is also the question of what you mean by "joining" in the question. If you mean explicit joins, there are ways around that -- such as correlated subqueries. However, these are implementing some form of "join" in the database engine.



          As for your query, you would want to use exists with a correlated subquery:



          select t1.*,
          (case when exists (select 1
          from table2 t2
          where t2.acc = t1.acc and
          datediff(month, t2.purchase_date, t1.call_date) = 1
          )
          then 'Yes' else 'No'
          end) as prior_month_purchase
          from table1 t1;


          This is "better" than a join because it does not multiply or remove rows. The result set has exactly the rows in the first table, with the additional column.



          The syntax assumes SQL Server (which was an original tag). Similar logic can be expressed in other databases, although date functions are notoriously database-dependent.






          share|improve this answer













          Of course you can have a query that references multiple tables without joining. union all is an example of an operator that does that.



          There is also the question of what you mean by "joining" in the question. If you mean explicit joins, there are ways around that -- such as correlated subqueries. However, these are implementing some form of "join" in the database engine.



          As for your query, you would want to use exists with a correlated subquery:



          select t1.*,
          (case when exists (select 1
          from table2 t2
          where t2.acc = t1.acc and
          datediff(month, t2.purchase_date, t1.call_date) = 1
          )
          then 'Yes' else 'No'
          end) as prior_month_purchase
          from table1 t1;


          This is "better" than a join because it does not multiply or remove rows. The result set has exactly the rows in the first table, with the additional column.



          The syntax assumes SQL Server (which was an original tag). Similar logic can be expressed in other databases, although date functions are notoriously database-dependent.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 12:34









          Gordon LinoffGordon Linoff

          771k35304406




          771k35304406

























              1














              Lets check the options,



              Say if you were to create a new third table on the basis of the data in first two, then every update/inserts/deletes to either of the tables should also propagate into the third table as well.



              Say you instead have a view which does what you need, there isnt a need to maintain that third table and also gets you the data needed from the first two each time you query it.



              create view third_table as
              select a.acc,a.call_date,case when dateadd(mm,-1,a.call_date)=b.purchase_date then 'Yes' else 'No end as prior_month_purchase
              from first_table a
              left join second_table b
              on a.acc=b.acc





              share|improve this answer




























                1














                Lets check the options,



                Say if you were to create a new third table on the basis of the data in first two, then every update/inserts/deletes to either of the tables should also propagate into the third table as well.



                Say you instead have a view which does what you need, there isnt a need to maintain that third table and also gets you the data needed from the first two each time you query it.



                create view third_table as
                select a.acc,a.call_date,case when dateadd(mm,-1,a.call_date)=b.purchase_date then 'Yes' else 'No end as prior_month_purchase
                from first_table a
                left join second_table b
                on a.acc=b.acc





                share|improve this answer


























                  1












                  1








                  1







                  Lets check the options,



                  Say if you were to create a new third table on the basis of the data in first two, then every update/inserts/deletes to either of the tables should also propagate into the third table as well.



                  Say you instead have a view which does what you need, there isnt a need to maintain that third table and also gets you the data needed from the first two each time you query it.



                  create view third_table as
                  select a.acc,a.call_date,case when dateadd(mm,-1,a.call_date)=b.purchase_date then 'Yes' else 'No end as prior_month_purchase
                  from first_table a
                  left join second_table b
                  on a.acc=b.acc





                  share|improve this answer













                  Lets check the options,



                  Say if you were to create a new third table on the basis of the data in first two, then every update/inserts/deletes to either of the tables should also propagate into the third table as well.



                  Say you instead have a view which does what you need, there isnt a need to maintain that third table and also gets you the data needed from the first two each time you query it.



                  create view third_table as
                  select a.acc,a.call_date,case when dateadd(mm,-1,a.call_date)=b.purchase_date then 'Yes' else 'No end as prior_month_purchase
                  from first_table a
                  left join second_table b
                  on a.acc=b.acc






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 6:30









                  George JosephGeorge Joseph

                  1,46559




                  1,46559






























                      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%2f53357925%2fnew-column-referencing-second-table-do-i-need-a-join%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings