Merge multiple tables in SSIS or SQL command - sql

I'm facing a problem with fetching data from multiple sources. It would be great if you can provide your ideas to design the SQL query.
I have to take data from two tables and INSERT it into a third table.
INPUT
TABLE 1
- TaskOrderNumer
- MemberID
TABLE 2
- ReferenceID
- MemberID
OUTPUT
TABLE 3
- TaskRefID
- PatID
My input table has TaskOrderNumber and MemberID. Right now I'm joining TABLE1 and TABLE2 based on MemberID. I'm getting the corresponding ReferenceID from TABLE2 and mapping it into PatID of TABLE3. The TaskOrder Number in TABLE1= TaskRefID in TABLE3.
I'm currently doing this using SSIS components. I want to make sure that the correct data is MERGED. I'm not able to map the TaskOrderNumber to TaskRefID. Can you please help me design the solution.

You can simply query the information you are trying to display. I am not sure I would even bother with SSIS here unless your sources aren't SQL.
select t1.TaskOrderID as TaskRefID
,t2.ReferenceID as PatID
into Table3 --Added this as edit.
from Table1 t1
join Table2 t2 on t1.MemberID=t2.MemberID

Related

Is it possible to update a local table with values from a linked table?

I have two identical tables in my database, T1 and T2. T1 is a local table while T2 is a linked table from a live SQL database. At the moment the two tables are identical.
In short, I would like to be able to run a query that will update T1 will all new records that have been added to T2. So once I have run the query, the two tables should be identical again. Is this at all possible? I need to have the data in T1 locally available as I need to be able to query that table even when the data in T2 is not available. The SQL database in question is off site so I will not always be able to run my queries as the link is unreliable.
Any assistance will be greatly appreciated.
If you do have a unique ID for every record and are sure records already entered will never be changed this can actually be quite simple:
INSERT INTO [TBL_INVOICES_LOCAL]
SELECT TBL_INVOICES.*
FROM [TBL_INVOICES_LOCAL] RIGHT JOIN TBL_INVOICES ON [TBL_INVOICES_LOCAL].InvoiceID = TBL_INVOICES.InvoiceID
WHERE ((([TBL_INVOICES_LOCAL].InvoiceID) Is Null));
All you need to do is join the two tables with the relationship being set to:
Include ALL records from LINKED_TABLE and only records from LOCAL_TABLE where the joined fields are equal.
Setting the criteria of the Local Tables ID field to "Is Null" will only show missing records. If you do this in an append query you can update your table in only one query as seen below:

SQL data migration

I'm just going to use an illustration to explain my problem. SQL Migration:
In the attached image are 2 SQL tables, Table 2 is referencing a primary key of table 1. Table 1 however had many duplicates so I deleted all the duplicates using Excel and imported the data into a new table with a new set of IDs. I now have to import table 2 into a new table and reference table 1 again like before. Now in the image it looks fairly easy to do that but I am dealing with a database of over 2000 rows after eliminating around 700 duplicates. Besides manually editing each row and matching them is there any way of doing this quickly. This is the first time I doing database migration but guessing there a quick ways of doing this. Google searches did not really bear any results. I appreciate any help on this.
To preview the data you need to delete from 2nd table please run this:
select * from names
where place_id NOT in (select id from places)
If you are happy with results you can run
delete from names
where place_id NOT in (select id from places)
after copying table2 into table2New
update table2New
set PlaceNameID = (select t1N.ID
from table2 as t2
join table1 as t1
on t1.ID = t2.PlaceNameID
join table1new as t1N
on t1.PlaceName = t1N.PlaceName
where table2New.ID = t2.ID
);

MS SQL Server 2017- Merging large tables automatically

Using Microsoft SQL server 2017, I want to merge two tables into a 3rd table.
Is there a way to have it defined automatically, without a query?
If it is not possible, I would like to mention that both tables contain a large amount of columns, therefore I look for an efficient query that will not make me write each column name.
Demonstration:
Original tables are Table1,Table2, column structure is as below:
Table1:
(Column1,Column2,Column3)
Table2:
(Column4,Column5,Column6)
My goal is to create Table3. Table3 is based on Table1 LEFT JOIN Table2 ON Column1=Column4.
Table 3:
(Column1,Column2,Column3,Column4,Column5,Column6)
Create it as a view.
CREATE VIEW table3 AS
SELECT Column1,Column2,Column3,Column4,Column5,Column6
FROM Table1 LEFT JOIN Table2 ON Column1=Column4
You can then reference table3 in other queries exactly like an ordinary table.

Create a big table with huge number of columns?

How can I create a table with 1000 row and at least 64000 columns. As I know, SQL database and Microsoft Excel suppot only 30000 and 16384 columns per table, respectively. How can I create such a table? Is there any solution? I want to use this table as an input for RapidMiner studio!
If you are determined to create this beast it would require a work around the column limit.
My suggestion would be 10x~6401 column tables with an ID/Key to link each row.
Join together the ten tables like so:
select * from
Table1
join Table2 on Table1.ID = Table2.ID
join Table3 on Table1.ID = Table3.ID
etc etc
Whether this would work as a singular input for RapiMiner, I'm not sure

Joining Different Database Tables

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.
You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you