Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:12:43+00:00 2026-05-27T09:12:43+00:00

Ok this is difficult to phrase, so here goes… I am using MS SQL

  • 0

Ok this is difficult to phrase, so here goes…

I am using MS SQL Server 2008 R2. I have a temp table that lets say has two already populated columns. There is a third empty column I want to populate based on the value of the first two columns. What I want to do is create a guid (using NEWUID()) for each matching combo of col1 and col2. Here is a visual example:

Lets say I have a temp table that looks like this initially:

Name    Activity    SpecialId
James   Running     
James   Running
James   Walking
John    Running
John    Running
John    Walking

I want it to get updated with new GUIDs so that it looks like this:

Name    Activity    SpecialId
James   Running     SOMEFAKEGUID_1
James   Running     SOMEFAKEGUID_1
James   Walking     SOMEFAKEGUID_2
John    Running     SOMEFAKEGUID_3
John    Running     SOMEFAKEGUID_3
John    Walking     SOMEFAKEGUID_4

Notice how a new GUID is created for each matching pair. So the James/Running combo has the same GUID for all James/Running combos… and the John/Running also has the same GUID for the John/Running combos, but not the same GUID as the James/Running combos do.

I tried to make that as clear as possible, but hopefully that isn’t clear as mud!

Can someone show me what the SQL query would look like in order to update that temp table with the correct GUIDs?

Thanks in advance.

Ryan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:12:44+00:00Added an answer on May 27, 2026 at 9:12 am

    Using NEWID() seems to be a pain. Using it a CTE creates a sperate ID, so you need some intermediary table instead.

    Declare @Table as table (name varchar(20), activity varchar(20) , SpecialID uniqueidentifier)
    Declare @DistinctTable as table (name varchar(20), activity varchar(20) , SpecialID uniqueidentifier)
    
    INSERT INTO @Table 
    (name, activity)
    values
    ('James','Running'),    
    ('James','Running'),
    ('James','Walking'),
    ('John','Running'),
    ('John','Running'),
    ('John','Walking')
    
    
    
    WITH distinctt 
         AS (SELECT DISTINCT name, 
                             activity 
             FROM   @Table) 
    INSERT INTO @DistinctTable 
    SELECT name, 
           activity, 
           Newid() 
    FROM   distinctt 
    
    UPDATE @Table 
    SET    specialid = dt.specialid 
    FROM   @Table t 
           INNER JOIN @DistinctTable dt 
             ON t.activity = dt.activity 
                AND t.name = dt.name 
    
    SELECT * FROM @Table 
    

    Produces

    name                 activity             SpecialID
    -------------------- -------------------- ------------------------------------
    James                Running              AAA22BC5-51FE-43B3-8CC9-4C4A5B4CC981
    James                Running              AAA22BC5-51FE-43B3-8CC9-4C4A5B4CC981
    James                Walking              1722B76B-5F17-4931-8D7C-2ECADB5A4DFD
    John                 Running              FBC1F86B-592D-4D30-ACB3-80DA26B00900
    John                 Running              FBC1F86B-592D-4D30-ACB3-80DA26B00900
    John                 Walking              84282844-AAFD-45CA-9218-F7933E5102C6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how difficult this but I have an array and would like
not sure why I find this so difficult. Here is the html code: <tr>
This is quite difficult to phrase, but I need to be able to link
Apologies for the rather confusing title, I'm finding it rather difficult to phrase this
I have heard that Java must use a JIT to be fast. This makes
A rather cryptic title, but it's difficult to phrase. Say I have an object
This question is pretty difficult for me to phrase well, so please bear with
Difficult to try and phrase this question, but I will try my best. Basically,
I'm just start using Sharepoint 2010 so this question is difficult for me. The
This is difficult to track down for me as a JS newbie. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.