combining data from two tables when no data overlaps - sql

I have two tables:
This is table1:
customer_name|customer_id|customer_phone|customer_birthday|(10 more columns that don't match with table2)
This is table2:
customer_name|customer_id|customer_phone|customer_status|(141 columns)
table1.customer_id is numeric
table2.customer_id is alphanumeric
table2 was imported from an old database and contains legacy customer data
How do I get data from both tables while only specifying the columns I want from each table.
The two tables have no overlapping data.
Please help me with this. I am not sure how to resolve it
Using SQL Server 2019

You could simply combine data from the both tables using UNION:
select customer_name, cast(customer_id as varchar), customer_phone, customer_birthday, null customer_status from table1
union all -- no need to do a useless distinct
select customer_name, customer_id, customer_phone, null as customer_birthday, customer_status from table2

As long as you have same column names, you can make a list of columns and use UNION like below:
SELECT col1, col2, col3, ...
FROM t1
UNION
SELECT col1, col2, col3
FROM t2;

Related

Create a view of a table with a column that has multiple values

I have a table (Table1) like the following:
Col1
Col2
First
Code1,Code2,Code3
Second
Code2
So Col2 can contain multiple values comma separated, I have another table (Table2) that contains this:
ColA
ColB
Code1
Value1
Code2
Vaue2
Code3
Vaue3
I need to create a view that joins the two tables (Table1 and Table2) and returns something like this:
Col1
Col2
First
Value1,Value2,Value3
Second
Value2
Is that possible? (I'm on Oracle DB if that helps.)
It's a violation of first normal form to have a list in a column value like that. It causes a lot of difficulties in a relational database, like the one you are encountering now.
However, you can get what you want by using the LIKE operator to find colA values that are substrings of the Col2 column. Add delimiters before and after to catch the first and last ones. Then aggregate back up to a single list using LISTAGG.
SELECT table1.col1,
LISTAGG(table2.colB,',') WITHIN GROUP (ORDER BY table2.colB) value_list
FROM table1,
table2
WHERE ','||table1.col2||',' LIKE '%,'||table2.colA||',%'
GROUP BY table1.col1
This will not perform well on large volumes, because without an equijoin it's going to use nested loops, and you can't use an index on a LIKE predicate with % at the beginning. The combination of nested loops + FTS is not pleasant with large volumes of data. Therefore, if this is your situation, you will need to fix the 1NF problem by transforming table1 into normal relational format, and then join it to table2 with an equijoin, which will enable it to use a hash join instead. So:
SELECT table1.col1,
LISTAGG(table2.colB,',') WITHIN GROUP (ORDER BY table2.colB) value_list
FROM (SELECT t.col1,
SUBSTR(t.col2,INSTR(t.col2,',',1,seq)+1,INSTR(t.col2,',',1,seq+1)-(INSTR(t.col2,',',1,seq)+1)) col2_piece
FROM (SELECT col1,
','||col2||',' col2
FROM table1) t,
(SELECT ROWNUM seq FROM dual CONNECT BY LEVEL < 10) x) table1,
table2
WHERE table1.col2_piece IS NOT NULL
AND table1.col2_piece = table2.colA
GROUP BY table1.col1
If you want the values in the same order in the list as the terms then you can use:
SELECT t1.col1,
LISTAGG(t2.colb, ',') WITHIN GROUP (
ORDER BY INSTR(','||t1.col2||',', ','||t2.colA||',')
) AS value2
FROM table1 t1
INNER JOIN table2 t2
ON INSTR(','||t1.col2||',', ','||t2.colA||',') > 0
GROUP BY
t1.col1
Which, for the sample data:
CREATE TABLE Table1 (Col1, Col2) AS
SELECT 'First', 'Code1,Code2,Code3' FROM DUAL UNION ALL
SELECT 'Second', 'Code2' FROM DUAL;
CREATE TABLE Table2 (ColA, ColB) AS
SELECT 'Code1', 'XXXX' FROM DUAL UNION ALL
SELECT 'Code2', 'ZZZZ' FROM DUAL UNION ALL
SELECT 'Code3', 'YYYY' FROM DUAL;
Outputs:
COL1
VALUE2
First
XXXX,ZZZZ,YYYY
Second
ZZZZ
fiddle

Union tables in Vertica SQL where tables have different order of columns?

I have 2 tables in Vertica SQL and how can I union all these tables to create tables as in result ? As you can see tables 1 and table 2 have different order of columns:
enter image description here
Just list the columns in the order you want them:
select col1, col2, col3
from table1
union all
select col2, col1, null
from table2;
Use SELECT INTO and UNION ALL would solve your problem
SELECT * INTO TEMP TABLE RESULT FROM TABLE1
UNION ALL
SELECT * FROM TABLE2

selecting entire table when selecting columns - How

Currently i have joined together several tables. now i want to select some columns out of this joined mesh.
my question is: is it possible to select all columns of a specific table?
for example:
select col1,col4,col7,all_columns.Table1,col9 from (joined tables including Table1)
if it is possible, how to implement it
this is what you want?
select col1,
col4,
col7,
Table1.*,
col9
from (joined tables including Table1)
How about this?
SELECT ... Table1.*, ... FROM ...

How do I concatenate two similar tables on a result

I have two tables with similar columns. I would simply like to select both tables, one after another, so that if I have 'x' rows on table1 and 'y' rows on table2, I'd get 'x + y' rows.
You would use UNION [ALL] for this. The tables don't need to have the same column names but you do need to select the same number of columns from each and the corresponding columns need to be of compatible datatypes
SELECT col1,col2,col3 FROM table1
UNION ALL
SELECT col1,col2,col3 FROM table2
UNION ALL is preferrable to UNION where there is a choice as it can avoid a sort operation to get rid of duplicates.
Just to add to what they were saying, you might want to add an Order By. Depends on the version of SQL you're using.
SELECT Col1, Col2, Col3
FROM Table1
UNION
SELECT Col1, Col2, Col3
FROM Table2
ORDER BY Col1
Note that ORDER and GROUP BYs have to go after the last table in the UNION.
select col1,col2,col3 from table1
union
select col1,col2,col3 from table2
Look at the Union operator.

SQL Append table queries

I have two tables, say table1 with two rows of data say row11 and row12
and table2 with 3 rows of data sat row21, row22, row23
Can anyone provide me with the SQL to create a query that returns
row11
row12
row21
row22
row23
Note: I dont want to create a new table just return the data.
Use UNION ALL, based on the example data:
SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2
UNION removes duplicates - if both tables each had a row whose values were "rowx, 1", the query will return one row, not two. This also makes UNION slower than UNION ALL, because UNION ALL does not remove duplicates. Know your data, and use appropriately.
select * from table1 union select * from table2
Why not use a UNION?
SELECT
Col1,Col2,Col3
FROM
TABLE1
UNION
SELECT
Col1,Col2,Col3
FROM
TABLE2
Are the columns on the two tables identical?
In MS Access you can achieve the same goal with an INSERT INTO query:
INSERT INTO TABLE1 SELECT * FROM TABLE2;