Impala - Get for multiple tables in database concentenated columns - impala

Follow up on this question: Impala - Get for all tables in database concentenated columns
Lets say I have a database A with tables B1, B2, ... B300.
B1 has columns C1 and C2
,B2 has columns D1, D2 and D3.
...
and B300 has columns E1 and E2.
I am looking for an Impala query that yields the following desired output:
B1 | "C1+C2"
B2 | "D1+D2+D3"
...
B300 | "E1+E2"
where "D1+D2+D3", "C1+C2" and "E1+E2" are concatenated strings.

First of all UNION all tables together and generate table_name for each table as you do this. You can copy your table names into excel and then automatically generate the SELECT and UNION statements for each table in excel (as a new column for each). Then you can run the UNION code in impala.
CREATE TABLE all_tables_unioned AS
SELECT
*
, "B1" AS table_name
FROM B1
UNION
SELECT
*
, "B2" AS table_name
FROM B2
Etc...
Then you can copy all of your column names in this new table from the hive metastore into excel and create a new column of commas (this is generating code in excel to save you typing the commas). Then copy and paste the two columns from excel into this code:
SELECT
CONCAT("all columns from excel")
FROM all_tables_unioned

Related

SQL Query to print columns instances in a new column

I don't know how to explain but i need a query to take this table:
Columns in the table
and print it like this:
Column to print
Is it possible?
Lots of ways to do this. Here's one:
SELECT a1 as a
FROM silly_table
UNION ALL
SELECT a2 as a
FROM silly_table
UNION ALL
SELECT a3 as a
FROM silly_table
ORDER BY 1

SQL query for selecting changes between 2 related columns in a single table

Is there a query that can select the rows where a change occurred between 2 related columns that hold different values?
To illustrate:
I have ONE table where there is an alphanumeric code, and another column to store its encrypted equivalent.
I need to find the rows where these 2 don't match. So if I have the following table "codes"
code | encryption
A1 | jl2
A1 | jl2
A1 | ki4
B2 | jl2
I want a query to select the rows where A1 resulted in ki4 and B2 resulted in jl2, because these 2 did not match to their usual results (A1 should always be jl2 and viceversa)
The codes are just examples of course, so I cannot just write a query that looks for these exact instances.
You can use not exists to get a list of the rows that don't have matches:
select distinct code, encryption
from t
where not exists (select 1
from t t2
where t2.code = t.code and
t2.encryption <> t.encryption
);
For those interested in an answer, I think this actually got it done. Basically compare the table to itself once and then select the instances where the other column doesnt match
select a.code,a.encryption,b.encryption
from codes a, codes b
where a.code = b.code
and a.encryption <> b.encryption

Copy data from one table to another - Ignore duplicates Postgresql

I am using Postgresql db. I have data in two tables. Table A has 10 records and Table B 5 records.
I would like to copy Table A data to Table B but only copy the new entries (5 records) and ignore the duplicates/already existing data
I would like to copy data from Table A to Table B where Table B will have 10 records (5 old records + 5 new records from Table A)
Can you please help me as to how can this be done?
Assuming id is your primary key, and table structures are identical(both table has common columns as number of columns and data type respectively), use not exists :
insert into TableB
select *
from TableA a
where not exists ( select 0 from TableB b where b.id = a.id )
If you are looking to copy rows unique to A that are not in B then you can use INSERT...SELECT. The SELECT statement should use the union operator EXCEPT:
INSERT INTO B (column)
SELECT column FROM A
EXCEPT
SELECT column FROM B;
EXCEPT (https://www.postgresql.org/docs/current/queries-union.html) compares the two result sets and will return the distinct rows present in result A but not in B, then supply these values to INSERT. For this to work both the columns and respective datatypes must match in both SELECT queries and your INSERT.
INSERT INTO Table_A
SELECT *
FROM Table_B
ON CONFLICT DO NOTHING
Here, the conflict will be taken based on your primary key.

SQL: How to dynamically loop & add N number of column with NULL value into temp table

Due to a certain requirement, I need to create two temp tables in Stored Procedure, after processing some data into them, I need to combine the two temp tables to show as one result set and generate into excel. So I'm thinking to use UNION when I want to show the final result set.
The issue is, the first temp table (Table A) is fixed to 20 columns, and the second temp table has 50 columns (Table B). My plan is, before processing data for Table A, I want to add 30 nullable columns and insert data for first 20 columns, and the rest is all NULL
After I process the data for Table B, I use UNION to combine Table A & B so that they will show as one result set.
What I can think of right now is to hard code some columns that are destined to have null values when I declare the temp table:
Declare #tmpTableA table (
....
ProcessDate datetime,
Mode int,
Col21 varchar(10)
Col22 varchar(10)
....
Col50 varchar(50)
)
When I insert data into Table A, I have to manually add null from Col21 onwards
Insert into(.... Col21, Col22, Col23....)
Values (.... NULL, NULL, NULL....)
After I complete processing data for Table A & B, I use UNION to merge Table A and B
Select *....Col49,Col50 From Table A
Union
Select *....CompleteDate,ContactPerson From Table B
Instead of hard-coding Col21 to Col50 into Table A, is there any elegant way to achieve that like using while loop to dynamically add N number of columns into Table A?
EDIT:
According to latest requirement, Table B has not only 50 columns but 100 columns! I really need a way to dynamically loop those columns rather than hard-coding for over 80 columns
I think you can just do
select * into #tableA from #tableB where 1=2
with this both tables will have same columns
You don't need to add columns to table A, just add 30 NULLs to select from Table A.
Select *,NULL,...,NULL,NULL From Table A
Union
Select * From Table B
You could add aliases to make the result a bit cleaner
Select *,...,NULL CompleteDate, NULL ContactPerson From Table A
Union
Select * From Table B

in SQLDeveloper, Build Where clause from excel sheet

I have spread sheet of one column has nearly half million records. To use that single column in a where clause, how smart or easily we can built? FYI - I'm using SQLDEV it allow only 1000 records in a single where clause.
Appreciate your help or suggestion.
Example :
Excel Column - SYS_ID
12
1
3
4
..
..
nearly half million sys_ID, i have to use this SYS_ID in a tableA in the where clause.
select * from tableA where sys_id in (12,1,3,4....)
Please suggest.
In excel, in the next column over, put this formula:
="UNION ALL SELECT "&B1&"
(where B1 is the cell where the first value is)
fill that down.
Now create a table variable:
declare #sysid table (sysid int)
insert into #sysid
-- paste your excel result here
-- remove the first "union all" manually
now you can use :
select * from tableA INNER JOIN #sysid s on sys_id=sysid