473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Merge data from one table into another

I have a database that I'm trying to simplify the design of but I'm struggling a little with it.

Just for illustration let's say I have two tables, TableA and TableB.

Each Row in TableA stores info for a type of document.

TableB stores info for who that document in TableA gets sent to. It allows the user to input as many people as needed per row in TableA but
after years of having this method, there are never more then two.

So I've created two fields in TableA to store that info instead of having Two Tables (this applies in many places and not just this simple Illustration).

The important columns for this illustration are is as follows:
TableA:
ID (PKEY)
SendTo_1 (Newly Created Column)
SendTo_2 (Newly Created Column)

TableB:
ID (PKEY)
TableA_ID (FKEY)
ContactID


I need to run a query that takes the ContactID data from TableB and insert it into TableA's sendTO_1 and SendTo_2 Fields respectively. It's easy if it's just one-to-one but it's not.
It's one-to-one and sometimes one-to-two depending on the doc.

How can I do this gracefully?
Nov 9 '11 #1
1 2437
ck9663
2,878 Expert 2GB
A straight-forward JOIN with a twist will do :)

Here, tweak this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. ;with sorted
  3. as
  4. (
  5.    select rowid = row_number() over(parition by id order by id, contactid), tablea_id, contactid
  6.    from tableB
  7. )
  8. insert into tablea(id, sendto_1, sendto_2)
  9. select
  10.    tablea_id, s1.contactid, s2.contactid
  11. from sorted s1
  12. left join sorted s2 on s1.tablea_id = s2.tablea_id and s1.rowid+1 = s2.rowid
  13.  
  14.  
Make sure your sendto_2 is NULLABLE.

Happy Coding!!!


~~ CK
Nov 10 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Baby Blue | last post by:
I have 2 codes below to grap data from another site. I use them to get the data from one News site. However, when I click on some link inside (such as :...
0
by: Chris Nighswonger | last post by:
------=_NextPart_000_0013_01C352C0.6B0A6E30 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi All, Is it possible in MySQL to use UPDATE to update...
1
by: Brian Mitchell | last post by:
Is it possible to merge a populated data table with another populated data table in .NET? I have 2 that I need combined into one. Thanks!!
1
by: Vasilis X | last post by:
Here is the question : I have a data table, UnShorted, which has a data column EventTime (type : date time) and a data column Values (type : single). I want to create a table, Shorted, that...
10
by: coffeesin | last post by:
Hi, I have a Table containing these fields: id-no, cold, cold ever, cold date,flu,flu ever and flu date. (The properties of id-no, cold,cold ever,flu,flu ever are set as Numbers.) What i want...
3
by: Ctal | last post by:
I have an app that populates several data tables on load. Each of these are bound to a datagrid. Above each datagrid I have several text boxes that display the data for the active row. There are...
2
parshupooja
by: parshupooja | last post by:
Hi, I have table which has 2 columns username password i want to insert username values from by copying data from another table whereas password shd be randomly generated Thank You
2
by: thread | last post by:
Hi All i'm building a database in access and i want to restrict permissions. from the access i can just limit the posiblity to unhide an hidden table and in this way i can preventing the users...
2
by: toofunny24 | last post by:
I have two tables and both have identical fields for the most part. Both tables are linked by product id. One table is used for entering actual data. Table two contains the specs/target numbers...
1
by: BaseballGraphs | last post by:
Hello, I am trying to divide one value from my data table with an associated ID by another value from my data table with a different ID for the same day that the value was added to the data table....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.