Copying data from one table to another different column names - sql

I'm having an issue copying one table's data to another. I have around 100 or so individual tables that have generally the same field names but not always. I need to be able to copy and map the fields. example: source table is BROWARD and has column names broward_ID, name, dob, address (the list goes on). The temp table I want to copy it to has ID, name, dob, address etc.
I'd like to map the fields like broward_ID = ID, name = name, etc. But many of the other tables are different in column name, so I will have to write a query for each one. Once I figure out the first on, I can do the rest. Also the column in both tables are not in order either..thanks in advance for the TSQL...

With tables:
BROWARD (broward_ID, name, dob, address) /*source*/
TEMP (ID, name, address,dob) /*target*/
If you want to copy information from BROWARD to TEMP then:
INSERT INTO TEMP SELECT broward_ID,NAME,ADDRESS,DOB FROM BROWARD --check that the order of columns in select represents the order in the target table
If you want only copy values of broward_ID and name then:
INSERT INTO TEMP(ID, name) SELECT broward_ID,NAME FROM BROWARD

Your question will resolve using update
Let's consider we have two different table
Table A
Id Name
1 abc
2 cde
Table B
Id Name
1
2
In above case want to insert Table A Name column data into Table B Name column
update B inner join on B.Id = A.Id set B.Name = A.Name where ...

Related

trying to match a column with a list of column names to a table that has those columns with their own values SQL

I have a table A that is a singular column called "Column_Name" and its values are a list of names. I have another table B whose columns are those values of "Column_Name" in table A. For example the values of "Column_Name" in table A are (Date, Age, Location). Then in table B, its actual columns are (Date, Age, Location) and those columns have their own respective values. I am trying to select the columns from table B that are values in "Column_Name" in table A. Is there a way to do this? I think I can use the IN operator in a WHERE clause, but not exactly sure how.
I don't know if I got it correctly,
Considering that the column_name in tableA is separated by commas,
would it be something like this?
select * from tableB
where Dt in (select SUBSTRING_INDEX(Column_Name,',',1) from tableA)
or Age in (select SUBSTRING_INDEX(Column_Name,',',2) from tableA)
or Location in (select SUBSTRING_INDEX(Column_Name,',',3) from tableA);
here is the example
https://www.db-fiddle.com/f/pUytYwKpK4mRsk76oFonV8/0

Why do i receive 'Duplicate column name' error when innerjoin-ing 2 tables with similar column name?

CREATE TABLE student_activestudent AS
(
SELECT *
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
I am expecting a table with 2 columns of studentnumber but I received Duplicate error instead --> Duplicate column name 'studentnumber'
A database table must have unique column names.
When you do select * you will get all columns from all tables and studentnumber exists on both student table and activestudent table. So to solve you problem specify the columns you want instead of *
CREATE TABLE student_activestudent AS
(
SELECT
student.studentnumber,
..Other columns..
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
At the very least, studentnumber is duplicated. In general, I strongly recommend that a view list all the columns explicitly. This protects the view if underlying columns change.
That said, if studentnumber is the only column, then you can do:
CREATE TABLE student_activestudent AS
SELECT *
FROM student s JOIN
activestudent ast
USING (studentnumber);
With using, the * does not repeat the join keys.
You cannot select two tables that have same column's name.
The best way is not to select *
Select by column and if the column is same you can put [as]
Example
SELECT student.studentnumber as stuNumber, activestudent.studentnumber as actstuNumber

Conditionally insert multiple data rows into multiple Postgres tables

I have multiple rows of data. And this is just some made up data in order to make an easy example.
The data has name, age and location.
I want to insert the data into two tables, persons and locations, where locations has a FK to persons.
Nothing should be inserted if there already is a person with that name, or if the age is below 18.
I need to use COPY (in my real world example I'm using NPQSQL for .NET and I think that's the fastest way of inserting a lot of data).
So I'll do the following in a transaction (not 100% sure on the syntax, not on my computer right now):
-- Create a temp table
DROP TABLE IF EXISTS tmp_x;
CREATE TEMPORARY TABLE tmp_x
(
name TEXT,
age INTEGER,
location TEXT
);
-- Read the data into the temp table
COPY tmp_x (name, age) FROM STDIN (FORMAT BINARY);
-- Conditionally insert the data into persons
WITH insertedPersons AS (
INSERT INTO persons (name, age)
SELECT name, age
FROM tmp_x tmp
LEFT JOIN persons p ON p.name = tmp.name
WHERE
p IS NULL
AND tmp.age >= 18
RETURNING id, name
),
-- Use the generated ids to insert the relational data into locations
WITH insertedLocations AS (
INSERT INTO locations (personid, location)
SELECT ip.id, tmp.location
FROM tmp_x tmp
INNER JOIN insertedPersons ip ON ip.name = tmp.name
),
DROP TABLE tmp_x;
Is there a better/easier/more efficient way to do this?
Is there a better way to "link" the inserts instead of INNER JOIN insertedPersons ip ON ip.name = tmp.name. What if name wasn't unique? Can I update tmp_x with the new person ids and use that?

Create table with other table data

Following query works but i want with 2 fields
CREATE TABLE TOTAPPS (ANUM) AS SELECT a.A# FROM APPLICANT a.
This is work that
//TOTAPPS
ANUM
--------
if i want to create with 2 fields?
//Totapps
ANUM NUMBER
---------------------------
how should i create the table? in order to get the correct out?
and the NUMBER is refer to the ANUM , for example
CREATE TABLE TOTAPPS (ANUM) AS SELECT a.A# FROM APPLICANT a,
(NUMBER) AS SELECT COUNT(*) FROM a.A#;
but it's failed to work.
Calling a column NUMBER is not correct as it is a reserved word. Assuming from your second query that you want to add to your table distinct values of a.A# and the count of these values in the table, you should try this:
CREATE TABLE TOTAPPS (ANUM, MYNUMBER) AS
SELECT a.A#, COUNT(a.A#) FROM APPLICANT a GROUP BY a.A#;

Transact-SQL: Getting ID of Lookup Value if Data isn't Related

I have 3 tables and two are related (Name and Gender):
Staging(id, name, gender)
Name(id, name genderID)
Gender(id, gender)
The data has been "dumped" into Staging(id, name, gender) in a denormalized fashion and now I'm trying to normalize the data.
I need to be able to use t-sql to do the following
Insert the name from the Staging table into the Name table
Get the id from the Gender table and insert into the Name table as a foreign key
The problem is the Gender and Name tables aren't related to Staging so I'm trying to understand the logic of how this transaction should work.
My assumption was that I needed to somehow to an INSERT INTO SELECT with some type of subquery, but I'm just at a lost. Thanks.
Yes, you need INSERT INTO...SELECT. Join table staging with Gender via column gender so you can get the ID.
INSERT INTO Name (ID, Name, GenderID)
SELECT s.id, s.name, g.id
FROM Staging s
INNER JOIN Gender g
ON s.gender = g.gender