T-SQL query to insert missing values in a table based on 2 columns combination criteria










0














I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:



Color Shape
---------------------
red square
red circle
red octagon
yellow square
yellow circle
yellow octagon
green square
green circle
green octagon


I have the following data table that contains "objects" as follows (there are no duplicates):



Object ID Shape Color
------------------------------------------------
object1 square green
object1 square red
object1 octagon yellow
object1 circle green
object2 circle red
object2 square yellow
object3 square red
object3 circle red
object3 square yellow
object3 square yellow
object3 octagon green
object4 circle red
etc......
etc......
object100


As you can see there are some "gaps" in terms of shape+color combination for each object.



What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:



 Object ID Color Shape
-------------------------------------------
object1 red square
object1 red circle
object1 red octagon
object1 yellow square
object1 yellow circle
object1 yellow octagon
object1 green square
object1 green circle
object1 green octagon
object2 red square
object2 red circle
object2 red octagon
object2 yellow square
object2 yellow circle
object2 yellow octagon
object2 green square
object2 green circle
object2 green octagon
etc......


Thank you










share|improve this question




























    0














    I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:



    Color Shape
    ---------------------
    red square
    red circle
    red octagon
    yellow square
    yellow circle
    yellow octagon
    green square
    green circle
    green octagon


    I have the following data table that contains "objects" as follows (there are no duplicates):



    Object ID Shape Color
    ------------------------------------------------
    object1 square green
    object1 square red
    object1 octagon yellow
    object1 circle green
    object2 circle red
    object2 square yellow
    object3 square red
    object3 circle red
    object3 square yellow
    object3 square yellow
    object3 octagon green
    object4 circle red
    etc......
    etc......
    object100


    As you can see there are some "gaps" in terms of shape+color combination for each object.



    What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:



     Object ID Color Shape
    -------------------------------------------
    object1 red square
    object1 red circle
    object1 red octagon
    object1 yellow square
    object1 yellow circle
    object1 yellow octagon
    object1 green square
    object1 green circle
    object1 green octagon
    object2 red square
    object2 red circle
    object2 red octagon
    object2 yellow square
    object2 yellow circle
    object2 yellow octagon
    object2 green square
    object2 green circle
    object2 green octagon
    etc......


    Thank you










    share|improve this question


























      0












      0








      0







      I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:



      Color Shape
      ---------------------
      red square
      red circle
      red octagon
      yellow square
      yellow circle
      yellow octagon
      green square
      green circle
      green octagon


      I have the following data table that contains "objects" as follows (there are no duplicates):



      Object ID Shape Color
      ------------------------------------------------
      object1 square green
      object1 square red
      object1 octagon yellow
      object1 circle green
      object2 circle red
      object2 square yellow
      object3 square red
      object3 circle red
      object3 square yellow
      object3 square yellow
      object3 octagon green
      object4 circle red
      etc......
      etc......
      object100


      As you can see there are some "gaps" in terms of shape+color combination for each object.



      What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:



       Object ID Color Shape
      -------------------------------------------
      object1 red square
      object1 red circle
      object1 red octagon
      object1 yellow square
      object1 yellow circle
      object1 yellow octagon
      object1 green square
      object1 green circle
      object1 green octagon
      object2 red square
      object2 red circle
      object2 red octagon
      object2 yellow square
      object2 yellow circle
      object2 yellow octagon
      object2 green square
      object2 green circle
      object2 green octagon
      etc......


      Thank you










      share|improve this question















      I have the following SQL Server 2016 table that holds all possible combination of 3 colors and 3 shapes:



      Color Shape
      ---------------------
      red square
      red circle
      red octagon
      yellow square
      yellow circle
      yellow octagon
      green square
      green circle
      green octagon


      I have the following data table that contains "objects" as follows (there are no duplicates):



      Object ID Shape Color
      ------------------------------------------------
      object1 square green
      object1 square red
      object1 octagon yellow
      object1 circle green
      object2 circle red
      object2 square yellow
      object3 square red
      object3 circle red
      object3 square yellow
      object3 square yellow
      object3 octagon green
      object4 circle red
      etc......
      etc......
      object100


      As you can see there are some "gaps" in terms of shape+color combination for each object.



      What I would like to achieve is to insert any missing combination of shape+color for each [object] record. Desired output for object1 and object2 would be for example:



       Object ID Color Shape
      -------------------------------------------
      object1 red square
      object1 red circle
      object1 red octagon
      object1 yellow square
      object1 yellow circle
      object1 yellow octagon
      object1 green square
      object1 green circle
      object1 green octagon
      object2 red square
      object2 red circle
      object2 red octagon
      object2 yellow square
      object2 yellow circle
      object2 yellow octagon
      object2 green square
      object2 green circle
      object2 green octagon
      etc......


      Thank you







      sql sql-server tsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 7:27









      marc_s

      570k12811021250




      570k12811021250










      asked Nov 10 at 1:17









      User 123

      205




      205






















          3 Answers
          3






          active

          oldest

          votes


















          1














          I think you can use this to insert your missing values in this way based on the output you have mentioned as well.



          create table color (colors varchar(10), Shape varchar(10))
          insert into color values
          ('red' , 'square' )
          ,('red' , 'circle' )
          ,('red' , 'octagon' )
          ,('yellow' , 'square' )
          ,('yellow' , 'circle' )
          ,('yellow' , 'octagon' )
          ,('green' , 'square' )
          ,('green' , 'circle' )
          ,('green' , 'octagon' )


          create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
          insert into objectsnew values
          ('object1' , 'square' , 'green' )
          ,('object1' , 'square' , 'red' )
          ,('object1' , 'octagon' , 'yellow' )
          ,('object1' , 'circle' , 'green' )
          ,('object2' , 'circle' , 'red' )
          ,('object2' , 'square' , 'yellow' )
          ,('object3' , 'square' , 'red' )
          ,('object3' , 'circle' , 'red' )
          ,('object3' , 'square' , 'yellow' )
          ,('object3' , 'square' , 'yellow' )
          ,('object3' , 'octagon' , 'green' )
          ,('object4' , 'circle' , 'red' )


          --Use this to make sure that you are inserting only unique combination for each object ID.



          insert into objectsnew (objectID, Shape, colors) 

          select o.objectID, c.Shape, o.colors from color c
          join objectsnew o on c.colors = o.colors
          except
          select objectID, Shape, colors from objectsnew
          order by o.objectID , o.colors


          Output



           objectID Shape colors
          object1 octagon green
          object1 circle green
          object1 square green
          object1 square red
          object1 octagon red
          object1 circle red
          object1 circle yellow
          object1 square yellow
          object1 octagon yellow
          Etc.....





          share|improve this answer




























            1














            You can try to use CROSS JOIN and DISTINCT



            SELECT t2.[Object ID],t1.*
            FROM
            (SELECT DISTINCT Color,Shape FROM T1) t1
            CROSS JOIN
            (SELECT DISTINCT [Object ID] FROM T2) t2


            sqlfiddle






            share|improve this answer




























              1














              The following generates the rows to be inserted:



              select cs.color, cs.shape, oi.objectID
              from colorshape cs cross join
              (select distinct o.objectID from objects o) oi
              where not exists (select 1
              from objects o
              where o.color = cs.color and
              o.shape = cs.shape and
              o.objectId = oi.objectId
              );


              You can add an insert before this to insert these into the table.






              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%2f53235201%2ft-sql-query-to-insert-missing-values-in-a-table-based-on-2-columns-combination-c%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









                1














                I think you can use this to insert your missing values in this way based on the output you have mentioned as well.



                create table color (colors varchar(10), Shape varchar(10))
                insert into color values
                ('red' , 'square' )
                ,('red' , 'circle' )
                ,('red' , 'octagon' )
                ,('yellow' , 'square' )
                ,('yellow' , 'circle' )
                ,('yellow' , 'octagon' )
                ,('green' , 'square' )
                ,('green' , 'circle' )
                ,('green' , 'octagon' )


                create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
                insert into objectsnew values
                ('object1' , 'square' , 'green' )
                ,('object1' , 'square' , 'red' )
                ,('object1' , 'octagon' , 'yellow' )
                ,('object1' , 'circle' , 'green' )
                ,('object2' , 'circle' , 'red' )
                ,('object2' , 'square' , 'yellow' )
                ,('object3' , 'square' , 'red' )
                ,('object3' , 'circle' , 'red' )
                ,('object3' , 'square' , 'yellow' )
                ,('object3' , 'square' , 'yellow' )
                ,('object3' , 'octagon' , 'green' )
                ,('object4' , 'circle' , 'red' )


                --Use this to make sure that you are inserting only unique combination for each object ID.



                insert into objectsnew (objectID, Shape, colors) 

                select o.objectID, c.Shape, o.colors from color c
                join objectsnew o on c.colors = o.colors
                except
                select objectID, Shape, colors from objectsnew
                order by o.objectID , o.colors


                Output



                 objectID Shape colors
                object1 octagon green
                object1 circle green
                object1 square green
                object1 square red
                object1 octagon red
                object1 circle red
                object1 circle yellow
                object1 square yellow
                object1 octagon yellow
                Etc.....





                share|improve this answer

























                  1














                  I think you can use this to insert your missing values in this way based on the output you have mentioned as well.



                  create table color (colors varchar(10), Shape varchar(10))
                  insert into color values
                  ('red' , 'square' )
                  ,('red' , 'circle' )
                  ,('red' , 'octagon' )
                  ,('yellow' , 'square' )
                  ,('yellow' , 'circle' )
                  ,('yellow' , 'octagon' )
                  ,('green' , 'square' )
                  ,('green' , 'circle' )
                  ,('green' , 'octagon' )


                  create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
                  insert into objectsnew values
                  ('object1' , 'square' , 'green' )
                  ,('object1' , 'square' , 'red' )
                  ,('object1' , 'octagon' , 'yellow' )
                  ,('object1' , 'circle' , 'green' )
                  ,('object2' , 'circle' , 'red' )
                  ,('object2' , 'square' , 'yellow' )
                  ,('object3' , 'square' , 'red' )
                  ,('object3' , 'circle' , 'red' )
                  ,('object3' , 'square' , 'yellow' )
                  ,('object3' , 'square' , 'yellow' )
                  ,('object3' , 'octagon' , 'green' )
                  ,('object4' , 'circle' , 'red' )


                  --Use this to make sure that you are inserting only unique combination for each object ID.



                  insert into objectsnew (objectID, Shape, colors) 

                  select o.objectID, c.Shape, o.colors from color c
                  join objectsnew o on c.colors = o.colors
                  except
                  select objectID, Shape, colors from objectsnew
                  order by o.objectID , o.colors


                  Output



                   objectID Shape colors
                  object1 octagon green
                  object1 circle green
                  object1 square green
                  object1 square red
                  object1 octagon red
                  object1 circle red
                  object1 circle yellow
                  object1 square yellow
                  object1 octagon yellow
                  Etc.....





                  share|improve this answer























                    1












                    1








                    1






                    I think you can use this to insert your missing values in this way based on the output you have mentioned as well.



                    create table color (colors varchar(10), Shape varchar(10))
                    insert into color values
                    ('red' , 'square' )
                    ,('red' , 'circle' )
                    ,('red' , 'octagon' )
                    ,('yellow' , 'square' )
                    ,('yellow' , 'circle' )
                    ,('yellow' , 'octagon' )
                    ,('green' , 'square' )
                    ,('green' , 'circle' )
                    ,('green' , 'octagon' )


                    create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
                    insert into objectsnew values
                    ('object1' , 'square' , 'green' )
                    ,('object1' , 'square' , 'red' )
                    ,('object1' , 'octagon' , 'yellow' )
                    ,('object1' , 'circle' , 'green' )
                    ,('object2' , 'circle' , 'red' )
                    ,('object2' , 'square' , 'yellow' )
                    ,('object3' , 'square' , 'red' )
                    ,('object3' , 'circle' , 'red' )
                    ,('object3' , 'square' , 'yellow' )
                    ,('object3' , 'square' , 'yellow' )
                    ,('object3' , 'octagon' , 'green' )
                    ,('object4' , 'circle' , 'red' )


                    --Use this to make sure that you are inserting only unique combination for each object ID.



                    insert into objectsnew (objectID, Shape, colors) 

                    select o.objectID, c.Shape, o.colors from color c
                    join objectsnew o on c.colors = o.colors
                    except
                    select objectID, Shape, colors from objectsnew
                    order by o.objectID , o.colors


                    Output



                     objectID Shape colors
                    object1 octagon green
                    object1 circle green
                    object1 square green
                    object1 square red
                    object1 octagon red
                    object1 circle red
                    object1 circle yellow
                    object1 square yellow
                    object1 octagon yellow
                    Etc.....





                    share|improve this answer












                    I think you can use this to insert your missing values in this way based on the output you have mentioned as well.



                    create table color (colors varchar(10), Shape varchar(10))
                    insert into color values
                    ('red' , 'square' )
                    ,('red' , 'circle' )
                    ,('red' , 'octagon' )
                    ,('yellow' , 'square' )
                    ,('yellow' , 'circle' )
                    ,('yellow' , 'octagon' )
                    ,('green' , 'square' )
                    ,('green' , 'circle' )
                    ,('green' , 'octagon' )


                    create table objectsnew (objectID varchar(20), Shape varchar(10),colors varchar(10))
                    insert into objectsnew values
                    ('object1' , 'square' , 'green' )
                    ,('object1' , 'square' , 'red' )
                    ,('object1' , 'octagon' , 'yellow' )
                    ,('object1' , 'circle' , 'green' )
                    ,('object2' , 'circle' , 'red' )
                    ,('object2' , 'square' , 'yellow' )
                    ,('object3' , 'square' , 'red' )
                    ,('object3' , 'circle' , 'red' )
                    ,('object3' , 'square' , 'yellow' )
                    ,('object3' , 'square' , 'yellow' )
                    ,('object3' , 'octagon' , 'green' )
                    ,('object4' , 'circle' , 'red' )


                    --Use this to make sure that you are inserting only unique combination for each object ID.



                    insert into objectsnew (objectID, Shape, colors) 

                    select o.objectID, c.Shape, o.colors from color c
                    join objectsnew o on c.colors = o.colors
                    except
                    select objectID, Shape, colors from objectsnew
                    order by o.objectID , o.colors


                    Output



                     objectID Shape colors
                    object1 octagon green
                    object1 circle green
                    object1 square green
                    object1 square red
                    object1 octagon red
                    object1 circle red
                    object1 circle yellow
                    object1 square yellow
                    object1 octagon yellow
                    Etc.....






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 2:07









                    Avi

                    384110




                    384110























                        1














                        You can try to use CROSS JOIN and DISTINCT



                        SELECT t2.[Object ID],t1.*
                        FROM
                        (SELECT DISTINCT Color,Shape FROM T1) t1
                        CROSS JOIN
                        (SELECT DISTINCT [Object ID] FROM T2) t2


                        sqlfiddle






                        share|improve this answer

























                          1














                          You can try to use CROSS JOIN and DISTINCT



                          SELECT t2.[Object ID],t1.*
                          FROM
                          (SELECT DISTINCT Color,Shape FROM T1) t1
                          CROSS JOIN
                          (SELECT DISTINCT [Object ID] FROM T2) t2


                          sqlfiddle






                          share|improve this answer























                            1












                            1








                            1






                            You can try to use CROSS JOIN and DISTINCT



                            SELECT t2.[Object ID],t1.*
                            FROM
                            (SELECT DISTINCT Color,Shape FROM T1) t1
                            CROSS JOIN
                            (SELECT DISTINCT [Object ID] FROM T2) t2


                            sqlfiddle






                            share|improve this answer












                            You can try to use CROSS JOIN and DISTINCT



                            SELECT t2.[Object ID],t1.*
                            FROM
                            (SELECT DISTINCT Color,Shape FROM T1) t1
                            CROSS JOIN
                            (SELECT DISTINCT [Object ID] FROM T2) t2


                            sqlfiddle







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 1:56









                            D-Shih

                            25.4k61431




                            25.4k61431





















                                1














                                The following generates the rows to be inserted:



                                select cs.color, cs.shape, oi.objectID
                                from colorshape cs cross join
                                (select distinct o.objectID from objects o) oi
                                where not exists (select 1
                                from objects o
                                where o.color = cs.color and
                                o.shape = cs.shape and
                                o.objectId = oi.objectId
                                );


                                You can add an insert before this to insert these into the table.






                                share|improve this answer

























                                  1














                                  The following generates the rows to be inserted:



                                  select cs.color, cs.shape, oi.objectID
                                  from colorshape cs cross join
                                  (select distinct o.objectID from objects o) oi
                                  where not exists (select 1
                                  from objects o
                                  where o.color = cs.color and
                                  o.shape = cs.shape and
                                  o.objectId = oi.objectId
                                  );


                                  You can add an insert before this to insert these into the table.






                                  share|improve this answer























                                    1












                                    1








                                    1






                                    The following generates the rows to be inserted:



                                    select cs.color, cs.shape, oi.objectID
                                    from colorshape cs cross join
                                    (select distinct o.objectID from objects o) oi
                                    where not exists (select 1
                                    from objects o
                                    where o.color = cs.color and
                                    o.shape = cs.shape and
                                    o.objectId = oi.objectId
                                    );


                                    You can add an insert before this to insert these into the table.






                                    share|improve this answer












                                    The following generates the rows to be inserted:



                                    select cs.color, cs.shape, oi.objectID
                                    from colorshape cs cross join
                                    (select distinct o.objectID from objects o) oi
                                    where not exists (select 1
                                    from objects o
                                    where o.color = cs.color and
                                    o.shape = cs.shape and
                                    o.objectId = oi.objectId
                                    );


                                    You can add an insert before this to insert these into the table.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 10 at 12:48









                                    Gordon Linoff

                                    756k35291399




                                    756k35291399



























                                        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%2f53235201%2ft-sql-query-to-insert-missing-values-in-a-table-based-on-2-columns-combination-c%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

                                        𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                        ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

                                        How do I collapse sections of code in Visual Studio Code for Windows?