TSQL - update column with duplicates in a brand new number set Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Select columns from result set of stored procedureHow do I generate random number for each row in a TSQL Select?Update all SQL NULL values in multiple columns using Column level WHERE clause?How to set a default value for an existing columnTSQL to number an ordered setTSQL: update a column to nullDuplicate Columns in TSQL PivotData mysteriously disappear from sql server tableAvoid key clash on SQL Merge Update statementTSQL bypass column on update

What causes the vertical darker bands in my photo?

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

A coin, having probability p of landing heads and probability of q=(1-p) of landing on heads.

Why was the term "discrete" used in discrete logarithm?

Fundamental Solution of the Pell Equation

Seeking colloquialism for “just because”

Are two submodules (where one is contained in the other) isomorphic if their quotientmodules are isomorphic?

How to align text above triangle figure

List *all* the tuples!

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Why aren't air breathing engines used as small first stages

Is it true that "carbohydrates are of no use for the basal metabolic need"?

How to bypass password on Windows XP account?

Why did the rest of the Eastern Bloc not invade Yugoslavia?

Can a USB port passively 'listen only'?

Sci-Fi book where patients in a coma ward all live in a subconscious world linked together

How to deal with a team lead who never gives me credit?

What's the meaning of 間時肆拾貳 at a car parking sign

How would the world control an invulnerable immortal mass murderer?

What does the word "veer" mean here?

Storing hydrofluoric acid before the invention of plastics

3 doors, three guards, one stone

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Single word antonym of "flightless"



TSQL - update column with duplicates in a brand new number set



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Select columns from result set of stored procedureHow do I generate random number for each row in a TSQL Select?Update all SQL NULL values in multiple columns using Column level WHERE clause?How to set a default value for an existing columnTSQL to number an ordered setTSQL: update a column to nullDuplicate Columns in TSQL PivotData mysteriously disappear from sql server tableAvoid key clash on SQL Merge Update statementTSQL bypass column on update



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have 4 columns in a table - 2 original, 2 new I added



O‌ne column is a document number, and a second column is the version. A document number may have multiple versions but will retain the same document number.‌



I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info.



I‌'m racking my brain as to how to code this?!?!



A‌ny help appreciated‌‌‌



D‌OC VERSION
1‌ 1
2‌ 1
3‌ 1
3‌ 2
3‌ 3
3‌ 4


I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info



D‌OC VERSION DOC2 VERSION2
1‌ 1 100 1
2‌ 1 101 1
3‌ 1 103 1
3‌ 2 104 2
3‌ 3 105 3
3‌ 4 106 4


W‌hat I want to do is update the DOC2 column to reflect the "min" docnumber of the set based on the original docnumber. For example,



D‌OC2 that has an original document number of 3 would all turn in to 103 like below



D‌OC VERSION DOC2 VERSION2
1‌ 1 100 1
2‌ 1 101 1
3‌ 1 103 1
3‌ 2 103 2
3‌ 3 103 3
3‌ 4 103 4









share|improve this question






























    0















    I have 4 columns in a table - 2 original, 2 new I added



    O‌ne column is a document number, and a second column is the version. A document number may have multiple versions but will retain the same document number.‌



    I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info.



    I‌'m racking my brain as to how to code this?!?!



    A‌ny help appreciated‌‌‌



    D‌OC VERSION
    1‌ 1
    2‌ 1
    3‌ 1
    3‌ 2
    3‌ 3
    3‌ 4


    I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info



    D‌OC VERSION DOC2 VERSION2
    1‌ 1 100 1
    2‌ 1 101 1
    3‌ 1 103 1
    3‌ 2 104 2
    3‌ 3 105 3
    3‌ 4 106 4


    W‌hat I want to do is update the DOC2 column to reflect the "min" docnumber of the set based on the original docnumber. For example,



    D‌OC2 that has an original document number of 3 would all turn in to 103 like below



    D‌OC VERSION DOC2 VERSION2
    1‌ 1 100 1
    2‌ 1 101 1
    3‌ 1 103 1
    3‌ 2 103 2
    3‌ 3 103 3
    3‌ 4 103 4









    share|improve this question


























      0












      0








      0








      I have 4 columns in a table - 2 original, 2 new I added



      O‌ne column is a document number, and a second column is the version. A document number may have multiple versions but will retain the same document number.‌



      I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info.



      I‌'m racking my brain as to how to code this?!?!



      A‌ny help appreciated‌‌‌



      D‌OC VERSION
      1‌ 1
      2‌ 1
      3‌ 1
      3‌ 2
      3‌ 3
      3‌ 4


      I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info



      D‌OC VERSION DOC2 VERSION2
      1‌ 1 100 1
      2‌ 1 101 1
      3‌ 1 103 1
      3‌ 2 104 2
      3‌ 3 105 3
      3‌ 4 106 4


      W‌hat I want to do is update the DOC2 column to reflect the "min" docnumber of the set based on the original docnumber. For example,



      D‌OC2 that has an original document number of 3 would all turn in to 103 like below



      D‌OC VERSION DOC2 VERSION2
      1‌ 1 100 1
      2‌ 1 101 1
      3‌ 1 103 1
      3‌ 2 103 2
      3‌ 3 103 3
      3‌ 4 103 4









      share|improve this question
















      I have 4 columns in a table - 2 original, 2 new I added



      O‌ne column is a document number, and a second column is the version. A document number may have multiple versions but will retain the same document number.‌



      I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info.



      I‌'m racking my brain as to how to code this?!?!



      A‌ny help appreciated‌‌‌



      D‌OC VERSION
      1‌ 1
      2‌ 1
      3‌ 1
      3‌ 2
      3‌ 3
      3‌ 4


      I‌ needed to be able to change all document numbers to a specific range, which was easy. I created a third column "doc2" and made another version column, retaining the same info



      D‌OC VERSION DOC2 VERSION2
      1‌ 1 100 1
      2‌ 1 101 1
      3‌ 1 103 1
      3‌ 2 104 2
      3‌ 3 105 3
      3‌ 4 106 4


      W‌hat I want to do is update the DOC2 column to reflect the "min" docnumber of the set based on the original docnumber. For example,



      D‌OC2 that has an original document number of 3 would all turn in to 103 like below



      D‌OC VERSION DOC2 VERSION2
      1‌ 1 100 1
      2‌ 1 101 1
      3‌ 1 103 1
      3‌ 2 103 2
      3‌ 3 103 3
      3‌ 4 103 4






      tsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 18:34









      Eric Brandt

      3,28511127




      3,28511127










      asked Mar 8 at 17:16









      Tim SeedorfTim Seedorf

      1




      1






















          3 Answers
          3






          active

          oldest

          votes


















          0














          Maybe I'm under-thinking this, but aren't you just looking for something along this line?



          UPDATE yourTable
          SET DOC2 = DOC + 99; --<Value at the bottom of your new range. 99 from the sample data.


          I see that 102 was skipped in your DOC2 sequence, so maybe there's some logic I'm not seeing.






          share|improve this answer






























            0














            I'm with @Eric in thinking that this is far simpler than it probably should be, but:



            UPDATE yourTable
            SET DOC2 = '10' + CAST(DOC AS VARCHAR)
            , Version2 = Version





            share|improve this answer






























              0














              Here's another option. You can use the MIN() function.



              DECLARE @TestData TABLE
              (
              [DOC] INT
              , [VERSION] INT
              , [DOC2] INT
              , [VERSION2] INT
              );

              INSERT INTO @TestData (
              [DOC]
              , [VERSION]
              , [DOC2]
              , [VERSION2]
              )
              VALUES ( 1, 1, 100, 1 )
              , ( 2, 1, 101, 1 )
              , ( 3, 1, 103, 1 )
              , ( 3, 2, 104, 2 )
              , ( 3, 3, 105, 3 )
              , ( 3, 4, 106, 4 );


              --Use MIN()
              SELECT *
              , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) --Give me the minimum DOC2 value for the DOC
              FROM @TestData;

              --You can not use a window function in a update statement
              --so either a CTE
              WITH [cte]
              AS ( SELECT [DOC]
              , [DOC2]
              , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) AS [MinDoc2]
              FROM @TestData )
              UPDATE [cte]
              SET [cte].[DOC2] = [cte].[MinDoc2];

              --or a sub query will do the job as well
              UPDATE [a]
              SET [a].[DOC2] = [b].[MinDoc2]
              FROM @TestData [a]
              INNER JOIN (
              SELECT [DOC]
              , MIN([DOC2]) AS [MinDoc2]
              FROM @TestData
              GROUP BY [DOC]
              ) [b]
              ON [b].[DOC] = [a].[DOC];

              SELECT * FROM @TestData





              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%2f55068000%2ftsql-update-column-with-duplicates-in-a-brand-new-number-set%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









                0














                Maybe I'm under-thinking this, but aren't you just looking for something along this line?



                UPDATE yourTable
                SET DOC2 = DOC + 99; --<Value at the bottom of your new range. 99 from the sample data.


                I see that 102 was skipped in your DOC2 sequence, so maybe there's some logic I'm not seeing.






                share|improve this answer



























                  0














                  Maybe I'm under-thinking this, but aren't you just looking for something along this line?



                  UPDATE yourTable
                  SET DOC2 = DOC + 99; --<Value at the bottom of your new range. 99 from the sample data.


                  I see that 102 was skipped in your DOC2 sequence, so maybe there's some logic I'm not seeing.






                  share|improve this answer

























                    0












                    0








                    0







                    Maybe I'm under-thinking this, but aren't you just looking for something along this line?



                    UPDATE yourTable
                    SET DOC2 = DOC + 99; --<Value at the bottom of your new range. 99 from the sample data.


                    I see that 102 was skipped in your DOC2 sequence, so maybe there's some logic I'm not seeing.






                    share|improve this answer













                    Maybe I'm under-thinking this, but aren't you just looking for something along this line?



                    UPDATE yourTable
                    SET DOC2 = DOC + 99; --<Value at the bottom of your new range. 99 from the sample data.


                    I see that 102 was skipped in your DOC2 sequence, so maybe there's some logic I'm not seeing.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 18:42









                    Eric BrandtEric Brandt

                    3,28511127




                    3,28511127























                        0














                        I'm with @Eric in thinking that this is far simpler than it probably should be, but:



                        UPDATE yourTable
                        SET DOC2 = '10' + CAST(DOC AS VARCHAR)
                        , Version2 = Version





                        share|improve this answer



























                          0














                          I'm with @Eric in thinking that this is far simpler than it probably should be, but:



                          UPDATE yourTable
                          SET DOC2 = '10' + CAST(DOC AS VARCHAR)
                          , Version2 = Version





                          share|improve this answer

























                            0












                            0








                            0







                            I'm with @Eric in thinking that this is far simpler than it probably should be, but:



                            UPDATE yourTable
                            SET DOC2 = '10' + CAST(DOC AS VARCHAR)
                            , Version2 = Version





                            share|improve this answer













                            I'm with @Eric in thinking that this is far simpler than it probably should be, but:



                            UPDATE yourTable
                            SET DOC2 = '10' + CAST(DOC AS VARCHAR)
                            , Version2 = Version






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 19:27









                            Junc MaylJunc Mayl

                            815




                            815





















                                0














                                Here's another option. You can use the MIN() function.



                                DECLARE @TestData TABLE
                                (
                                [DOC] INT
                                , [VERSION] INT
                                , [DOC2] INT
                                , [VERSION2] INT
                                );

                                INSERT INTO @TestData (
                                [DOC]
                                , [VERSION]
                                , [DOC2]
                                , [VERSION2]
                                )
                                VALUES ( 1, 1, 100, 1 )
                                , ( 2, 1, 101, 1 )
                                , ( 3, 1, 103, 1 )
                                , ( 3, 2, 104, 2 )
                                , ( 3, 3, 105, 3 )
                                , ( 3, 4, 106, 4 );


                                --Use MIN()
                                SELECT *
                                , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) --Give me the minimum DOC2 value for the DOC
                                FROM @TestData;

                                --You can not use a window function in a update statement
                                --so either a CTE
                                WITH [cte]
                                AS ( SELECT [DOC]
                                , [DOC2]
                                , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) AS [MinDoc2]
                                FROM @TestData )
                                UPDATE [cte]
                                SET [cte].[DOC2] = [cte].[MinDoc2];

                                --or a sub query will do the job as well
                                UPDATE [a]
                                SET [a].[DOC2] = [b].[MinDoc2]
                                FROM @TestData [a]
                                INNER JOIN (
                                SELECT [DOC]
                                , MIN([DOC2]) AS [MinDoc2]
                                FROM @TestData
                                GROUP BY [DOC]
                                ) [b]
                                ON [b].[DOC] = [a].[DOC];

                                SELECT * FROM @TestData





                                share|improve this answer



























                                  0














                                  Here's another option. You can use the MIN() function.



                                  DECLARE @TestData TABLE
                                  (
                                  [DOC] INT
                                  , [VERSION] INT
                                  , [DOC2] INT
                                  , [VERSION2] INT
                                  );

                                  INSERT INTO @TestData (
                                  [DOC]
                                  , [VERSION]
                                  , [DOC2]
                                  , [VERSION2]
                                  )
                                  VALUES ( 1, 1, 100, 1 )
                                  , ( 2, 1, 101, 1 )
                                  , ( 3, 1, 103, 1 )
                                  , ( 3, 2, 104, 2 )
                                  , ( 3, 3, 105, 3 )
                                  , ( 3, 4, 106, 4 );


                                  --Use MIN()
                                  SELECT *
                                  , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) --Give me the minimum DOC2 value for the DOC
                                  FROM @TestData;

                                  --You can not use a window function in a update statement
                                  --so either a CTE
                                  WITH [cte]
                                  AS ( SELECT [DOC]
                                  , [DOC2]
                                  , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) AS [MinDoc2]
                                  FROM @TestData )
                                  UPDATE [cte]
                                  SET [cte].[DOC2] = [cte].[MinDoc2];

                                  --or a sub query will do the job as well
                                  UPDATE [a]
                                  SET [a].[DOC2] = [b].[MinDoc2]
                                  FROM @TestData [a]
                                  INNER JOIN (
                                  SELECT [DOC]
                                  , MIN([DOC2]) AS [MinDoc2]
                                  FROM @TestData
                                  GROUP BY [DOC]
                                  ) [b]
                                  ON [b].[DOC] = [a].[DOC];

                                  SELECT * FROM @TestData





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Here's another option. You can use the MIN() function.



                                    DECLARE @TestData TABLE
                                    (
                                    [DOC] INT
                                    , [VERSION] INT
                                    , [DOC2] INT
                                    , [VERSION2] INT
                                    );

                                    INSERT INTO @TestData (
                                    [DOC]
                                    , [VERSION]
                                    , [DOC2]
                                    , [VERSION2]
                                    )
                                    VALUES ( 1, 1, 100, 1 )
                                    , ( 2, 1, 101, 1 )
                                    , ( 3, 1, 103, 1 )
                                    , ( 3, 2, 104, 2 )
                                    , ( 3, 3, 105, 3 )
                                    , ( 3, 4, 106, 4 );


                                    --Use MIN()
                                    SELECT *
                                    , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) --Give me the minimum DOC2 value for the DOC
                                    FROM @TestData;

                                    --You can not use a window function in a update statement
                                    --so either a CTE
                                    WITH [cte]
                                    AS ( SELECT [DOC]
                                    , [DOC2]
                                    , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) AS [MinDoc2]
                                    FROM @TestData )
                                    UPDATE [cte]
                                    SET [cte].[DOC2] = [cte].[MinDoc2];

                                    --or a sub query will do the job as well
                                    UPDATE [a]
                                    SET [a].[DOC2] = [b].[MinDoc2]
                                    FROM @TestData [a]
                                    INNER JOIN (
                                    SELECT [DOC]
                                    , MIN([DOC2]) AS [MinDoc2]
                                    FROM @TestData
                                    GROUP BY [DOC]
                                    ) [b]
                                    ON [b].[DOC] = [a].[DOC];

                                    SELECT * FROM @TestData





                                    share|improve this answer













                                    Here's another option. You can use the MIN() function.



                                    DECLARE @TestData TABLE
                                    (
                                    [DOC] INT
                                    , [VERSION] INT
                                    , [DOC2] INT
                                    , [VERSION2] INT
                                    );

                                    INSERT INTO @TestData (
                                    [DOC]
                                    , [VERSION]
                                    , [DOC2]
                                    , [VERSION2]
                                    )
                                    VALUES ( 1, 1, 100, 1 )
                                    , ( 2, 1, 101, 1 )
                                    , ( 3, 1, 103, 1 )
                                    , ( 3, 2, 104, 2 )
                                    , ( 3, 3, 105, 3 )
                                    , ( 3, 4, 106, 4 );


                                    --Use MIN()
                                    SELECT *
                                    , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) --Give me the minimum DOC2 value for the DOC
                                    FROM @TestData;

                                    --You can not use a window function in a update statement
                                    --so either a CTE
                                    WITH [cte]
                                    AS ( SELECT [DOC]
                                    , [DOC2]
                                    , MIN([DOC2]) OVER ( PARTITION BY [DOC] ) AS [MinDoc2]
                                    FROM @TestData )
                                    UPDATE [cte]
                                    SET [cte].[DOC2] = [cte].[MinDoc2];

                                    --or a sub query will do the job as well
                                    UPDATE [a]
                                    SET [a].[DOC2] = [b].[MinDoc2]
                                    FROM @TestData [a]
                                    INNER JOIN (
                                    SELECT [DOC]
                                    , MIN([DOC2]) AS [MinDoc2]
                                    FROM @TestData
                                    GROUP BY [DOC]
                                    ) [b]
                                    ON [b].[DOC] = [a].[DOC];

                                    SELECT * FROM @TestData






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 8 at 20:29









                                    Tim MylottTim Mylott

                                    1,27819




                                    1,27819



























                                        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%2f55068000%2ftsql-update-column-with-duplicates-in-a-brand-new-number-set%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

                                        1928 у кіно

                                        Захаров Федір Захарович

                                        Ель Греко