sql select into columns to table and columns - sql

I have a source table that is NewID|Fruit|Apples and I need to insert those rows into target table OldID|Fruit|Apples
Is there a way to select into a new table while changing columns?
Thanks!

Try:
CREATE TABLE new_table SELECT NewID as OldID, Fruit, Apples FROM old_table

Assuming target table already exists and you want to copy all the columns
INSERT INTO target_table(OldID,Fruit,Apples)
SELECT NewID,Fruit,Apples FROM source_table

Related

Hive- Delete duplicate rows using ROW_NUMBER()

How to delete duplicates using row_number() without listing all the columns from the table. I have a hive table with 50+ columns. If i want to delete duplicates based on a 2 columns below are the steps i followed
Create temp table as Create temptable as select * from (select
*,row_number() over(col1,col2) as rn from maintable) where rn=1)
Insert overwrite table maintable select * from temptable
But here in insert it fails because the new column rn is present in temptable; To avoid this column i would have to list all the rest of the columns.
And there is no Drop column option in hive. There also you need to use REPLACE function which again needs listing all the rest of the columns.
So any better idea for deleting duplicates in Hive based on 2 columns?
Spell out all column names from the original table for insert overwrite as the query computes a new column. No temp table is needed for this.
Insert overwrite table maintable
select col1,col2,col3 ---...col50
from (select t.*
,row_number() over(order by col1,col2) as rn
from maintable
) t
where rn = 1

Insert into table using select plus value?

I created a table modeled on an existing table:
CREATE TABLE new_table AS
(SELECT * FROM old_table WHERE record_status = 'xyz' ) WITH DATA;
I then added a column:
ALTER TABLE new_table ADD new_column VARCHAR(100);
I now want to select more rows from the old table into the new table, but add a fixed value (eg. 'blabla') to the new column for each row selected. I tried a bunch of things, such as:
INSERT INTO new_table
SELECT *, 'blabla'
FROM old_table)
WHERE is_blabla = 'Y'
Tried every permutation I could think of but keep getting syntax errors. Any suggestions? I'm using Teradata.
Dave
INSERT INTO new_table
SELECT old_table.*,'your_fixed_value'
FROM old_table
WHERE is_blabla='Y'
You just need to alias your table
INSERT INTO new_table
SELECT a.*,'your_fixed_value'
FROM old_table a
WHERE is_blabla='Y'

duplicate rows using insert into

I have a table that contains data and I want to use insert into statement
here is my query
INSERT INTO [table]
SELECT Distinct * FROM [DataSource]
how to to ignore a row from being inserted if it's already found in the table?
Assuming your target table and your data source table have identical structure, you could do something like this:
INSERT INTO [table]
SELECT Distinct *
FROM [DataSource]
EXCEPT
SELECT *
FROM [table]
Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.

copy data from one table to another existing table using sql

I know we could use
Insert Into table_name(cloumn_names)
Select column_names
From table_name
but how about Select Into? Can we also use Select Into to copy data from one table to another EXISTING table using? I mean can we specify the column names of the latter table using Select Into?
Thx!
You can use insert into select syntax
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
It basically selects columns from table1 and respectively insert in table 2
SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
Inserting Rows by Using SELECT INTO
You can try this :
UPDATE [TestDB].[dbo].[Table1]
SET [Content] = (
Select [TestDB].[dbo].[Table2].[Content] from Table2 where ID= 'XYZ')
WHERE [ID] = 'XYZ'
GO

Copy rows from one table to another in MySQL

I have two tables with the same structure. I need to select data from one table and have to store them into the other.
How can I do that?
insert into tablea(id,name) select id,name from tableb;
Since they are the same structure then you can just do
insert into table1 select colum1, column2,... from table2
insert into blacklist
select *
from newblacklist
for uploading any single number
insert into blacklist
select *
from newblacklist
where numbers ='123456'
leave out the values keyword
insert into tbl1
select * from tbl2