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 3677364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:12:45+00:00 2026-05-19T03:12:45+00:00

Got a slightly tricky problem I’d like some advice on. I have a source

  • 0

Got a slightly tricky problem I’d like some advice on. I have a source SQL table that I need to separate into two separate tables. The source contains a bunch of columns, including a postal address.

What I need to do is add the rows containing first instance of any address to the first table, Then any subsequent occurrences of the same address (if any) are to be added to the second table, with an additional column containing the ID of the row in the first table (for joining purposes).

Can anyone suggest how to do this? I’d prefer to keep it all within T-SQL (MS SQL Server 2008), but if necessary I can knock together some C# as part of the process.

  • 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-19T03:12:46+00:00Added an answer on May 19, 2026 at 3:12 am

    You should be able to create a suitable CTE (Common Table Expression) do divide those addresses apart. You didn’t provide much details – so I tried to make up some column and table names – adapt those as needed.

    Try this – it will delete all duplicates from your table:

    ;WITH DuplicateAddr AS
    (
        SELECT 
           (list of columns you need),
           ROW_NUMBER() OVER (PARTITION BY Street,ZipCode,City
                              ORDER BY DateLastChange DESC) 'RowNum'
        FROM dbo.Addresses
    )
    SELECT (list of columns) 
    FROM DuplicateAddr
    WHERE RowNum > 1
    GO
    

    With this CTE, you “partition” your data according to a list of columns – use those you need to use. Each partition of data (e.g. each address defined by Street,ZipCode,City) will get sequential numbers, based on some ordering (that you define in the CTE – I chose DateLastChange as one possibility).

    So the rows with RowNum = 1 make up the set of “first” addresses – instead of just a SELECT (columns) FROM DuplicateAddr, you can of course also do an INSERT INTO .... based on that criteria.

    All the rows with a RowNum > 1 are the “duplicate” or additional addresses – insert those into your second table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a little problem that's slightly frustrating. Is it possible to set a
I've got a slightly tricky problem to solve; imagine this: One of my applications
I have a tricky problem that has had me scratching my head for a
Guys, I have got a slightly different problem with Java Dynamic class loading. I
So I've got this: date('c') which formats it like so: 2010-08-17T08:55:14-07:00 but I need
Got a class that serializes into xml with XMLEncoder nicely with all the variables
Got a problem with ADOMD.NET 8.0, SQL2008 and our app. It isn't giving us
Got a bit of a mind freeze at the moment. I have the following
I've got a slightly hackish makefile for running tests: ### Run the tests tests
I've got some image processing code in C++ which calculates gradients and finds straight

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.