Azure SQL Create new table from merging two others using a custom ID column - sql

Im new to configuring Azure SQL but im trying to create a new table by merging some columns from two tables which both share the same Custom ID column.
For examples my data is like the following
CustomID
InfoColumn1
InfoColumn2
UNIQUEID001
InfoColumnData123
TEST10001afssf
UNIQUEID002
InfoColumnData456
TEST10001acetca
UNIQUEID003
InfoColumnData789
TEST10001bsgtse
And
CustomID
OtherColumn1
OtherColumn2
UNIQUEID001
Blah2142214asfasf
a4a545a545a
UNIQUEID002
Blah436346sadsasf
aa45a4w555a5a
UNIQUEID003
Blah43643373bsegags
asgsgsgsag
And i want to create a table like the following
CustomID
InfoColumn1
OtherColumn1
UNIQUEID001
InfoColumnData123
Blah2142214asfasf
UNIQUEID002
InfoColumnData456
Blah436346sadsasf
UNIQUEID003
InfoColumnData789
Blah43643373bsegags
Am i going about this the right way or do i have to create a PowerAutomate Flow? and use a condition of ID eq ID?
Also my data is quite large (40k unique IDs)

Related

Get generated key from column organized table in DB2 Warehouse

I am not able to get generated key from column organized table from an INSERT SQL statement run against IBM DB2 warehouse. I am using Java and JDBC driver. Everything works fine - I am able to connect to DB, create tables, insert data, I am just not able to get a generated key if it is generated in column organized table. Note that row organized tables work fine and return the key properly.
Consider a table:
CREATE TABLE users (
id INTEGER not null GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
username VARCHAR(16),
PRIMARY KEY (id)
);
If this is row organized table I am able to get the generated key fine by using:
PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", PreparedStatement.RETURN_GENERATED_KEYS);
However, If this is column organized table the PreparedStatemnt creation fails with an error:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13
Even if I specify columns I want to get returned like so:
PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", new String[]{"id","username"});
pr.setString(1, "test");
pr.executeUpdate();
I get
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13
on line pr.executeUpdate();.
Does this mean that it is not possible to get generated key from column organized table from the INSERT statement in DB2 Warehouse?
Currently shipping versions v11.1.x and V11.5.x will throw SQL1667N when the query sent to Db2 uses 'FINAL TABLE' or 'OLD TABLE', or 'NEW TABLE' clauses for a column organized table.
When you use the jdbc syntax PreparedStatement.RETURN_GENERATED_KEYS, this syntax may be used under the covers.
Currently those clauses are not supported (i.e. will cause the exception to be thrown) for ORGANIZE BY COLUMN tables. There are other restrictions on column organized tables that you should be aware of before using them.
You can workaround this by creating your tables explicitly with the ORGANIZE BY ROW clause.
Have you tried actually selecting the generated ID? Try something like this:
SELECT ID FROM FINAL TABLE
(INSERT INTO users(username) VALUES(?))
See "Retrieval of result sets from an SQL data change statement" in the IBM Db2 documentation.

How to make a new copy of table without CREATE TABLE but using INSERT

I'm using SQL Server 2014 and trying to figure out some not trivia task.
I have a table PROPERTY and need to copy some of the data to absolutely new table PROPERTY_1 (PROPERTY_1 is not created and I'm not allowed to use CREATE TABLE). I have to use INSERT only!
I've googled some fancy commands like INSERT INTO from MySQL:
INSERT INTO PROPERTY_1 SELECT * FROM PROPERTY
but it's no help because (surprise-surprise!) PROPERTY_1 is not created.
Is it any possible way to pass this task or it's just some kind of weird task?
You must use Select Into as described in the Microsoft: documentation
You can use Select into to create the structure only and use Insert into to copy the data like below:
SELECT * INTO PROPERTY_1 FROM PROPERTY WHERE 1=0
INSERT INTO PROPERTY_1 SELECT * FROM PROPERTY
Benefits of using Select into to create the structure of the copy table:
You don't need to worry about the columns and their data types in the new table (PROPERTY_1) as those will be similar to the base table (PROPERTY). The new table schema will match the original schema, including identity columns.
There won't be any errors while inserting the records in the new table.

Can we add column to an existing table in AWS Athena using SQL query?

I have a table in AWS Athena which contains 2 records. Is there a SQL query using which a new column can be inserted in to the table?
You can find more information about adding columns to table in Athena documentation
Or you can use CTAS
For example, you have a table with
CREATE EXTERNAL TABLE sample_test(
id string)
LOCATION
's3://bucket/path'
and you can create another table from sample_test with the query
CREATE TABLE new_test
AS
SELECT *, 'new' AS new_col FROM sample_test
You can use any available query after AS
This is mainly for future readers like me, who was struggling to get this working for Hive table with AVRO data and if you don't want to create new table i.e updating schema of the existing table. It works for csv using 'add columns', but not for Hive + AVRO. For Hive + AVRO, to append columns at the end, before partition columns, the solution is available at this link. However, there are couple of things to note that, we need to pass full schema to the literal attribute and not just the changes; and (not sure why but) we had to alter hive table for all 3 things in the same order - 1. add columns using add columns 2. set tblproperties and 3. set serdeproperties. Hopefully it helps someone.

How can I copy a Redshift table but add a sortkey to a column?

I'm currently working on a project that uses a Redshift table with 51 columns. However, the person who made the table forgot to add a sortkey to our time column which will hurt performance for our use case if we don't add it.
How can I make a version of the table with our time column as the sortkey? I'm aware that you can't make a column a sortkey if its a member of an existing table, but I was hoping there's a way to do it that doesn't involve writing out the CREATE TABLE syntax by hand; for example, something like this would be nice:
timecube=# CREATE TABLE foo (like bar) sortkey(time);
ERROR: CREATE TABLE LIKE is not supported with DISTSTYLE, DISTKEY(), or SORTKEY() clauses
but as you can see its not supported. Is there another way? As we're still developing we don't need any of existing data.
Using traditional tools like pgdump didn't work well because they don't include any of the Redshift extras like encoding.
Redshift supports specifying the DIST and SORT keys as part of CREATE TABLE AS statements, as per the docs.
CREATE TABLE table_name
DISTSTYLE KEY
DISTKEY ( column )
SORTKEY ( column )
AS
(SELECT *
FROM source_table)
;
First step you need to do use get create table statement for existing table. Then create new table this time add sort key to new table.
Check encoding for old table ( when you load data using copy command it automatically adds compression encodings)
select "column", type, encoding
from pg_table_def where tablename = 'old_table'
When creating new table add encoding type for each column. Create table with Sort key .
Once new table is created use below command
insert into new table ( select * from old table order by time asc)

How to create a table from the attributes of another table that contain a key word?

I have a SQL table called SCUBA_CLASSES with attributes ID and NAME. In the table are different kinds of scuba diving classes from beginner to advanced levels. There is a second table called REQUIRED_BOOKS that has the attributes ID and TITLE that contain all the books required by different scuba classes.
I'd like to create a new table called INTRO_REQUIREMENTS with an attribute ID. It will contain a list of all the books required by scuba classes whose name starts with the word "Introduction". How would you create this?
So far I have:
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID;
I can generate a list of classes with required books but can't figure out how to add the requirement that the name of the class has to start with "Introduction".
add one more condition in where clause using using AND and Search name using LIKE.
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID
AND name LIKE "introduction%";