Extract all tables and respective columns from long SQL Query - sql

The task I am trying to solve is to get all tables out of a long SQL query and its respective columns.
E.g.
SELECT
t1.id, t1.gender, t1.name,
t2.age, t2.salary
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
Wanted output:
{'table1': ['id', 'gender', 'name'], 'table2': ['age', 'salary']}
I considered using string splitting etc. getting all table names and based on the alias (if available) get the columns.
But this is getting way to complicated if there are multiple joins and maybe also UNIONs.
Is there an available library or smart way to do that?

If it's only for 1 query I would advise to use MS Excel and filter on the table alias. Generate the select statement via MS Excel and you could create something like this:
SELECT
'table1:', t1.id, t1.gender, t1.name,
'table2:',t2.age, t2.salary
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

In case if this helps.
Take the column name from All_TAB_COLUMN and Pivot it. Still this is not exact result you want.
select * from (
select TABLE_NAME,COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME in
('Table1','Table2'))
pivot
(
max(table_name)
for COLUMN_NAME in ('gender','name','age','salary')
)
order by 1;

Related

How to join a table with itself with two records per id?

So, I have a table with the following structure:
id
columnA
columnB
1
Yes
1
No
I want to combine the row into a single row, so it ends up like this:
id
columnA
columnB
1
Yes
No
I believe a self join here would work like this:
SELECT t1.columnA , t2.columnB
FROM table1 t1, table1 t2
where t1.id = t2.id
But is there a way to do this without specifying the columns? I have a table that has 100 columns and I'm trying to see if I can accomplish this without listing out all the columns.
Use the below query to get the column name with aggregation (Query created using information schema to get the column names). Write a select using the result and run the query.
select
case when column_name='Id' then column_name
else concat(',Max(', column_name,')') end as Name
from information_schema.columns
where table_name = 'Table1';
You will get something like below as output, where A and B are the column names.
Id
,Max(A)
,Max(B)
Add convert the result to query
Select
Id
,Max(A)
,Max(B)
from Table1 Group by Id
is there a way to do this without specifying the columns?
You can use using to answer your question.
SELECT t1.columnA , t2.columnB
FROM table1 t1 JOIN
table1 t2
USING (id);
To get the data you want, use aggregation:
SELECT id, MAX(t1.columnA), MAX(t2.columnB)
FROM table1 t1
GROUP BY id;
Use your JOIN only change the colums for *
SELECT t1.*, t2.*
FROM table1 t1, table1 t2
where t1.id = t2.id

SQL Select statement (from 2 different tables)

Heyy I'm new to sql and I'd just like to know if there's a way to retrieve select statements with conditions from other tables.
I want to select all name values that have a number that identifies that they have committed a crime. I only want to select a name once.
"SELECT distinct * FROM Table1 WHERE number LIKE table2.number "
Are you looking for IN?
SELECT t1.*
FROM Table1 t1
WHERE t1.number IN (SELECT t2.number FROM table2 t2 t2.number);
Under most circumstances, the rows in a table should be unique. So, you don't need SELECT DISTINCT. The DISTINCT can add a considerable amount of overhead to such a query.
You can able to use INNER JOIN like below,
select tbl1.Name from tableOne tbl1
inner join tableTwo tbl2 ON tbl1.commonKey = tbl2.commonKey
where tbl1.columnName = 'any value'

Unable to understand query

I am working on an SSIS job that contains a complex query.
It has some thing like :
some sql statements
left outer join
(
select query joining two more tables )
table1
here, i am unable to understand what that table1 mean ? Is it a kind of temporary view
created . This table1 is used in the other parts of query . But, actually the table1 does
exists in the database.
Is it like , the results of the select query in the parenthesis is created as table1
Please clarify me on this..
I am not able to put down my code because of Security Policies
Here is SQL Fiddel example
Below is the sample query
Select Temp1.id,Table1.id Table1_id
from Temp1
left Outer join
(
Select Temp2.id
from Temp2
join Temp3
On Temp2.id = Temp3.id
) Table1
on Temp1.id = Table1.Id
In above example table1 is the Alias for data coming from joinsof two tables (temp2 and temp3)
table1 is an alisas your subquery. It's the name of subquery you can use with columns for example table1.col1
It is an alias for the query in the parenthesis.
If you would remove that you would get an error.
Aliases are also good when you have the same column in more than on joined tables, so you can distinquish them.
For instance if colX is both in Table1 and Table2 you would have a query like:
SELECT T1.colX,T2.colX
FROM Table1 T1
JOIN Table2 T2
ON T1.id = T2.id

Is there some way to select the first table without writing all the fields names in the query?

Q: If I have two tables joined together, and I want to get (*) from the first table, I mean all the fields of the first table.
Shall I write all the fields names of the first table in the query? Or is there some way to select * just from the first table.
You should not to list all the fields, see the example:
SELECT DISTINCT t1.*
from T1
join t2 on condition
first_table.* should do the trick.
Sounds more like a semi join. Consider rewriting e.g.
SELECT DISTINCT T1.*
FROM T1 JOIN T2 ON T1.id = T2.id;
can be re-written as
SELECT *
FROM T1
WHERE id IN (SELECT id FROM T2);

Oracle given column names

I have 2 tables with 2 columns (user_id and year).
Query1:
SELECT * FROM table_1 t1
FULL JOIN table_2 t2 ON t1.user_id=t2.user_id AND t1.year=t2.year
Produces following column names:
user_id, year, user_id_1, year_1
Query2:
CREATE TABLE table_copy AS SELECT * FROM
(SELECT * FROM table_1 t1
FULL JOIN table_2 t2 ON t1.user_id=t2.user_id AND t1.year=t2.year);
Produces following vague column names:
QCSJ_C000000000400000, QCSJ_C000000000400002, QCSJ_C000000000400001, QCSJ_C000000000400003
Is there a short way to force Oracle query2 to use the same names as query1 without writing them explicitly (it is important when there are many columns)? Maybe some Oracle settings?
List your columns and use AS to specify the column name.
e.g.
CREATE TABLE table_copy AS
SELECT t1.user_id AS t1_user_id,
t1.year AS t1_year,
t2.user_id AS t2_user_id,
t2.year AS t2_year
FROM table_1 t1
FULL JOIN table_2 t2 ON t1.user_id=t2.user_id
AND t1.year=t2.year;
I didn't fully understand your expected part but what I understood is you want all columns from table_1 or from table_2 only
if it is like this only you can use following query to create table ..
CREATE TABLE table_copy AS SELECT * FROM
(SELECT t1.* FROM table_1 t1 FULL JOIN table_2 t2
ON t1.user_id=t2.user_id
AND t1.year=t2.year);
or if you want both table's column but with different name in sql then you have to follow query suggested by cagcowboy only....
but you can create table with prefix like "t1_" in plsql without specify or write all column name..