create table to compare rows - sql

In order to be able to use UTL_MATCH.JARO_WINKLER_SIMILARITY in oracle, i would need to have a table(temp table in my case) in following format
a ----------------abc
a ----------------bax
a ----------------tax
b ----------------abc
b ----------------bax
b ----------------tax
c ----------------abc
c ----------------bax
c ----------------tax
I have column in the LEFT coming from one table and the column in RIGHT from another table. My question is how to create table in such format?
I would highly appreciate your help.

If you want empty structure then:
create table test as
select table1.col1, table2.col2
from table1, table2
where 1=0
If you want table with data then:
create table test as
select distinct table1.col1, table2.col2
from table1, table2

Related

create a view in HIVE merge two tables

How can I create a view to merge three tables
The workflow is like initially one table was created in mysql now this table has been divided to 3 tables and kept in hive
so for that I need to create a view
Initially in mysql one table for eg the table name is Initialtable.
This Initialtable consists of col1,col2,col3,col4,col5
now this table has been divided to 3 tables in hive and I need to merge these tables using a view
1)table1
2)table2
3)table3
Now this table1 consists of col1,col3,col5
table2 consists of col1,col2,col3
table3 consists of col1,col5
Now I have to create a view so that I can merge these table1,table2,table3
for that I will put the non used columns in table1,table2,table3 as null
like create view v1 select col1,col2 as null,col3,col4,col5 from table1 union select col1,col2,col3,col4 as null,col5 as null from table 2 union col1,col2 as null,col3 as null, col4 as null,col5 from table 3
can someone provide a proper syntax to gain this output in hive
Assuming table1, table2, table3 are the three tables which were split and the columns are as below:
table1: col1,col3,col5
table2: col1,col2,col3
table3: col1,col4,col3
and col1 is the primary key across all the three tables. You can create a view as below:
CREATE OR replace VIEW initialtable AS
SELECT DISTINCT a.col1,
b.col2,
a.col3,
c.col4,
a.col5
FROM TABLE1 AS a
join TABLE2 AS b
ON ( a.col1 = b.col1 )
join TABLE3 AS c
ON ( c.col1 = a.col1 )

How to Select all columns of a table except one

My code looks like:
CREATE TABLE tableC AS
(SELECT tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM tableB, tableA
JOIN tableB
ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2);
Now It is working but I have two columns geom and geom2!
Inside geom column I will have the new geometry based on the intersection. So how can I select tableA except the geom column?
Create the table with all the columns and after that drop the geom column and rename the new one:
CREATE TABLE tableC AS
SELECT
tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM
tableA inner JOIN tableB ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2
;
alter table tableC drop column geom;
alter table tableC rename column geom2 to geom;
The only way you would be able to do this would be to generate a dynamic SQL statement based on the columns within the table that excludes those you don't want. Obviously this will be a lot more effort than simply adding in all the column names.
There are also a lot of very good reasons to never include a select * in a production environment, given how picky SQL often is on the number and format of columns that are returned. By using select * you open yourself up to a changing query result in the future that could potentially break things.
If you have a LOT of columns and you simply don't want to manually type them all out, run the query below for your table and then format the result so you can copy/paste into your script:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'

Trying to Merge data from one table to another

Trying to do a process where I have a table that is existing. This is table A
I have staging table which I have uploaded the data. This is table B.
Table A already contains data in there apart from some data which i need added from table B to Table A.
So Table B has a extra column which I need it to match with the data already existing in Table A.
So layout currently in Table A is:
Column 1 Column 2 Column 3
AB
ABC
Layout in Table B is:
Column 1 Column 2 Column 3
ABC Yellow Test1
AB Blue Test2
So I need these columns 2 and 3 moved to table A from Table B, so they match correctly with the data that is already in column 1.
Tried my best to explain and english is my 2nd language, so i apologise for any mistakes. Anyway know what best way to go with this, would it be a merge?
Thanks
Update TableA
Set TableA.Column2=B.Column2,
TableA.Column3=B.Column3
From TableA A
Inner Join TableB B ON B.Column1=A.Column1
Use Left Outer join to merge data
SELECT TableA.clomun1,
Tableb.column2.tableb.column3
FROM tableA
LEFT JOIN tableB
ON table1.column1 = table2.column1;
Following query is working fine in MySQL , try it out :
update TableA as a inner join TableB as b on a.Column1=b.Column1
set a.Column2 = b.Column2 , a.Column3=b.Column3
where a.Column1=b.Column1;
And for Oracle use this one :
update TableA
set TableA.Column2 =
(select TableB.Column2 from TableB where TableA.Column1=TableB.Column1),
TableA.Column3 =
(select TableB.Column3 from TableB where TableA.Column1=TableB.Column1);
MERGE INTO TableA
USING (SELECT Column1, COlumn2, Column3
FROM TableB) Q
ON (TableA.Column1=TableB.Column1)
WHEN MATCHED THEN UPDATE SET TableA.Column2=Q.Column2
, TableA.Column3=Q.Column3;
This works fine in Oracle. You can also insert records that are not in Table A adding
WHEN NOT MATCHED THEN INSERT (Column1, Column2, Column3)
VALUES (Q.Column1, Q.Column2, Q.Column3)

Include table name in column from select wildcard sql

Is it possible to include table name in the returned column if I use wildcard to select all columns from tables?
To explain it further. Suppose I want to join two tables and both tables have the column name “name” and many other columns. I want to use wildcard to select all columns and not explicitly specifying each column name in the select.
Select *
From
TableA a,
TableB b
Where
a.id = b.id
Instead of seeing two column with same name "name", could I write a sql to return one column name as "a.name" (or TableA.name) and one as "b.name"(or TableB.name) without explicitly putting the column name in select?
I would prefer a solution for mssql but other database could be a reference too.
Thanks!
You can use select a.*, ' ', b.* from T1 a, T2 b to make it more visible where columns from T1 end and columns from T2 begin.
You are basically joining two tables on the ID field, so you will only see one column labeled "ID", not two, because you are asking to see only those records where the ID is the same in table a and table b: they share the same id.
Try ...
SELECT 'TableA' AS 'Table', A.* FROM TableA A
WHERE A.id IN (SELECT id FROM TableB)
UNION
SELECT 'TableB' AS 'Table', B.* FROM TableB B
WHERE B.id IN (SELECT id FROM TableA)
ORDER BY id, [Table]

copying fields from multiple tables to create new table

How to create a table by copying only the columns from multiple tables??
eg. t1 with fields (A1,A2,A3)
t2 with fiedls (D1,D2,D3)
t3 with fields (Z1,Z2,Z3)
Now I've to create a new table using only the fields not the values from the above three tables i.e.
new_tbl (A1,A2,A3,D1,D2,D3,Z1,Z2,Z3)
How can I do it??
try this:
create table new_tbl as
select *
from t1
join t1 on 1=2
join t3 on 1=2
the condition 1=2 is always false.. so it will not return any data but the column header..