How to transfer data from one table to another randomly - sql

Is it possible to put random values from table1.column1 (5rows) into table2.column2 (17 rows)?
What's more: column2 in table2 was added using alter table, so I have specific rows with ID and missing data only in that one added column.
example
image here

Related

(PostgreSQL) Stored Procedure to move data from Table1 to Table 2

How would you move all the data in Table1 to Table2 on PostgreSQL using a stored procedure when Table1 and Table2 have different column names (cannot simply JOIN).
Assume both tables are already made in the DB and the purpose is once data from Table1 is moved to Table2, Table1 gets wiped (using a trigger?), and assume Table2 is blank before any data is moved from Table1.
Please refer to image below for a simplified example.
Was thinking using a cursor in some way or form. Would that be efficient?
Much thanks!
Click for image sample

Copy related rows from two tables into those same tables with new values in SQL?

I have two tables, "table1" and "table2". They contain rows that are related to each other. The table "table2" has pairs of rows with a column named "table1_id" and these pairs refer to the sequential "id" columns in "table1".
The task that needs to be performed is that I need to copy rows from both tables and put these rows into the same tables with new data while maintaining the relationships between them.
I have read several posts on StackOverflow and some articles on "mssqltips.com", but I am still not sure how I should do this. Should I use a cursor or a query with joins and a temporary table? What is the best-practice way to achieve the above task and if possible, could you demonstrate a short example?
You cannot "copy both rows from both tables and put these into the same tables" verbatim if the id column is being copied and is truly an ID (a unique key). If you want to duplicate some other (non-key) columns into new rows with new IDs, that's fine. If that's what you want, you can create the primary key (table1) row(s) first, and reference the newly created keys. How you do this depends on whether the id column is a generated or explicitly specified key, and whether you have any keys (primary or foreign keys) in table2 as well.
It might look something like this:
insert into table1
select col1, col2, etc
from table1
where id = 'somekey'
insert into table2
select 'newkey' as table1_id, col3, col4, etc
from table2
where table1_id = 'somekey'
If you want to do this atomically, wrap it in a begin transaction, and make use of an OUTPUT clause or similar to collect the new IDs; see the documentation about that.

How to update numerical column of one table based on matching string column from another table in SQL

I want to update numerical columns of one table based on matching string columns from another table.i.e.,
I have a table (let's say table1) with 100 records containing 5 string (or text) columns and 10 numerical columns. Now I have another table that has the same structure (columns) and 20 records. In this, few records contain updated data of table1 i.e., numerical columns values are updated for these records and rest are new (both text and numerical columns).
I want to update numerical columns for records with the same text columns (in table1) and insert new data from table2 into table1 where text columns are also new.
I thought of taking an intersect of these two tables and then update but couldn't figure out the logic as how can I update the numerical columns.
Note: I don't have any primary or unique key columns.
Please help here.
Thanks in advance.
The simplest solution would be to use two separate queries, such as:
UPDATE b
SET b.[NumericColumn] = a.[NumericColumn],
etc...
FROM [dbo].[SourceTable] a
JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
INSERT INTO [dbo].[DestinationTable] (
[NumericColumn],
[StringColumn1],
[StringColumn2],
etc...
)
SELECT a.[NumericColumn],
a.[StringColumn1],
a.[StringColumn2],
etc...
FROM [dbo].[SourceTable] a
LEFT JOIN [dbo].[DestinationTable] b
ON a.[StringColumn1] = b.[StringColumn1]
AND a.[StringColumn2] = b.[StringColumn2] etc...
WHERE b.[NumericColumn] IS NULL
--assumes that [NumericColumn] is non-nullable.
--If there are no non-nullable columns then you
--will have to structure your query differently
This will be effective if you are working with a small dataset that does not change very frequently and you are not worried about high contention.
There are still a number of issues with this approach - most notably what happens if either the source or destination table is accessed and/or modified while the update statement is running. Some of these issues can be worked around other ways but so much depends on the context of how the tables are used that it is difficult to provide a more effective generically-applicable solution.

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

Exporting data from spreadsheet to Pgsql database

I have one huge spreadsheet file (1000+ lines) and one postgreSQL table. I need to compare spreadsheet data with postgresql table data and add fill the blank fields in table with data from spreadsheet and add entries not present in db table.
Yes, I can convert (via csv) whole spreadsheet into database table. But, there are unique values in both documents, so I will lose data doing this. Or, is it possible to compare 2 tables and fill missing fields in table A with data from table B?
Thanks in advance!
It's easy in SQL to compare two tables, and insert rows not in one table. For example:
INSERT INTO TableA
(col1, col2, col3)
SELECT
col1, col2, col3
FROM SpreadSheetTable
WHERE NOT EXISTS (
SELECT *
FROM TableA
WHERE TableA.col1 = SpreadSheetTable.col1
)
This query inserts all rows from SpreadSheetTable into TableA, except those rows for which TableA already contains a row with the same "col1" value.