MS SQL Server 2017- Merging large tables automatically - sql

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.

Related

HQL query to get all the columns from 2 tables

I have 2 tables, let's say Table1 and Table2. Both the tables have more than 50 columns. I want to write a query which returns all the columns. After that I will create one model class and map the columns returned from query result which has around 60 columns from both the tables.
Below are the sample columns for 2 tables. I have not made any mappings in entities in java classes.
Table1
id
search_key
col3 and so on.. 50+ cols
Table2
id
t1_id
col3 and so on.. 50+ cols
I have written one native query as below:
#Query(value="SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.t1_id WHERE t1.search_key IN ('12345')", nativeQuery=true)
But this native sql query is throwing NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native sql query.
My Requirement:
I need to write a query either native or hql which returns all the columns from both the tables based on matching criteria. and then that returned object by query I will map the columns which I want (since I want around 60 columns from both the tables, I don't want to write each column in select statement)
Is there a way to achieve this???
Please let me know if you need more info in question.
Actually if you run your query on a SQL client you can see that it returns all columns (from both tables) so we can understand that the problem is that your query is Associated with an Entity or generally a Java class that you have created. If you run your query and save the result on a List<Object> and not on a List<YourCustomObject> in your code then you will see that every Object of your list would contain all rows from both tables. Your SQL query as a plain sql query returns all rows from both tables as it is.

Merging 2 SQL tables with same table convention design without using update command

I have 2 tables in my SQL database:
And I want to merge them in a way the result will be:
This is just an example for 2 tables which need to be merged into one new table (The tables contain an example data, the statement should work for any amount of data inside the tables).
The ID which got different value in CSV should be updated into the new table for example:
ID 3's value is 'KKK' and in table T is 'CCC', then what should be updated is the CSV table.
You seem to want a left join and to match to the second table if available:
select t.id, coalesce(csv.value, t.value) as value
from t left join
csv
on t.id = csv.id;
If you want this in a new table, use the appropriate construct for your database, or use insert to insert into an existing table.

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:

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

Merge multiple tables in SSIS or SQL command

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