How to join all Oracle tables in new table - sql

I have an oracle DB with a lot of tables (more than 60).
For Example:
Table 1
ID STRING DATA
1 ABC READ
2 CDE WRITE
3 FGH READ
4 HSS WRITE
5 FFH WRITE
Table 2
ID Name Feb
1 Deven 12
4 Monish 21
5 Ritesh 22
Table 3
ID STRING DATA READY
1 ABC READ OK
2 CDE WRITE NO
3 FGH READ OK
I need to create a new table where for each ID It shows all the fields and informations contained in all the db tables for that ID. All in the same row.:
NEW TABLE I NEED TO CREATE
ID STRING DATA Name Feb READY
1 ABC READ Deven 12 OK
2 CDE WRITE NO
3 FGH READ OK
4 HSS WRITE Monish 21
5 FFH WRITE Ritesh 22
Which comand Have I to use?
Please be patient because I’m a newbe of sql language.
Thank you very much

Basically, you want a big left outer join starting with the first table (based on your sample data):
select t1.id, t1.string, t1.data, t2.name, t2.feb, t3.ready
from table1 t1 left join
table2 t2
on t1.id = t2.id left join
table3 t3
on t1.id = t3.id;
It is impossible to tell from your sample data if the id is the only (or right) join key. However, you should (and need to) know the relationships between the tables in order to create the correct query.
If you want this in a table, just pre-pend the create table as statement before the select.

Create a view which has the fields that you want. Then execute INSERT INTO ... SELECT * FROM view ....

Related

How do I find unmatched records with a table that contains comma separated values

I am trying to check if the values from Table1 exist in Table2.
The thing is that the values are comma separated in Table1
Table 1
ID
TXT
1
129(a),P24
2
P112
3
P24,XX
4
135(a),135(b)
Table 2
ID
P24
P112
P129(a)
135(a)
135(b)
The following only works if the complete cell value exists in both tables:
SELECT Table1.ID, Table1.TXT
FROM Table1 LEFT JOIN Table2 ON Table1.[TXT] = Table2.[ID]
WHERE (((Table2.ID) Is Null));
MY QUESTION IS:
Is there a way to check each comma separated value and return those that do not exists in Table 2.
In above example the value XX should end up in the result.
Not sure why you store your data in that way (which is bad practice as sos mentioned above), but you need to mimic the temp table like in SQL server.
Select from table1 and create different txt rows per id.
Insert the results from section 1 into the table3.
Select from table3 and join it to table2.
Delete table 3.
Table3 the temp table
ID
TXT
1
129(a)
1
P24
2
P112
3
P24
3
XX
4
135(a)
4
135(b)
Here is some explanation MS Access database (2010) how to create temporary table/procedure/view from Query Designer

acessing columns relevant to dynamic table in oracle

i have been having trouble solving what seems to me a complex aspect of plsql, I have a table1 with a list of table_names and a table2 with backups to those table_names
Table1
id name max_rows date
1 a 100 2018-10-06
2 b 100 2018-10-06
3 c 100 2018-10-06
Table2
id name_bck FK date_created date closed
1 a_bck 1
2 b_bck 2
3 c_bck 3
so the idea is for me to insert rows from a onto a_bck until a_bck reaches its limit (max_rows) then i would create a new a_bck2 and close(update close date from table2);
the problem im having is i cant access columns from table a of my table1, i want to insert data from a to a_bck where its date column from table a is < then sysdate
so i created 2 cursors to go through both tables and return the data i need to perform operations over.
cursor c1 is
select id name max_rows from table1 where date<sysdate;
curso2 c2 is
select id_fk , name, from table2 where table1.id=table2.id_fk and close_date is null;
then i would loop through and fetch the data related to table1 and table2
fetch c1 into id, n_tab, n_rows;
FETCH c2 INTO id_fk, n_tab2;
I would like some help on how to dynamically access the columns from the tables on table1.
I tried to summarize the best way possible.
If anyone could show me a small example of how i could implement this.
PS: i cant use partitions
Thank you in advance

Insert records into records into table from 2 difference source in SQL Server

Just wondering will it be possible to insert records into a table from 2 difference sources in SQL?
Example:
Table 1
Number
1
2
Table 2
Name
Alex
Amy
I want to insert records into table 3 from table 1 and table 2 and the result for table 3 should be:
Number Name
1 Alex
2 Alex
1 Amy
2 Amy
Any way that I can do it in SQL Server?
Try a CROSS JOIN and a SELECT ... INTO:
This join relates every-with-every row. The result will be filled into a new table on the fly:
SELECT Nrs.Nr
,Nms.Name
INTO dbo.TheNewTable
FROM dbo.NumberTable AS Nrs
CROSS JOIN dbo.NameTable AS Nms;
See the result:
SELECT * FROM dbo.TheNewTable;
connect for two connections .
use a script not only in SQL :javca for example.
use hashmap and hashet ...
isnert in temporary table(drop on commit for example).
copy in table 3.
-don't forget to close connections.

How to concatenate two tables with matched columns into a single view

I have two tables in a Microsoft Access 2007 database.
One of them has columns "name 1 & name 2" with 2000 records. The other table has more information: it has two columns named "code1 & code2" with 3000 records. Each name has a code means that every name has a code stored in the second table in the column code 1.
I want to make one table or query that shows me name 1 & name 2 with their specific code1 & code 2 from the second table .
And it must have only 2000 records
example :
tabel 1 :
name-1 name-2
-----------------------------
Abacavir Digoxin
Amprenavir Aspirin
tabel 2 :
code-1 drug1
----------------
xy1 Abacavir
xy2 Digoxin
yxr1 Amprenavir
uyv2 Aspirin
sample output :
name-1 code-1 name-2 code-2
-----------------------------------------
Abacavir xy1 Digoxin xy2
This table structure is poorly designed but I'll assume you are stuck with it. You have to create a query that joins Table2 twice... once for each of the relationships you are linking by. In my test I created an access db and named my tables simple Table1 and Table2 so you may need to alter this query slightly to match your table/column names.
SELECT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
The fact you have 2K in the first and 3K in the second is a clue. Try adding the 'Distinct' key in your SQL statement:
SELECT DISTINCT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
Otherwise I think Frank nailed it.

MySQL Query That Can Pull the Data I am Seeking?

On the project I am working on, I am stuck with the table structure from Hades. Two things to keep in mind:
I can't change the table structure right now. I'm stuck with it for the time being.
The queries are dynamically generated and not hard coded. So, while I am asking for a query that can pull this data, what I am really working toward is an algorithm that will generate the query I need.
Hopefully, I can explain the problem without making your eyes glaze over and your brain implode.
We have an instance table that looks (simplified) along these lines:
Instances
InstanceID active
1 Y
2 Y
3 Y
4 N
5 Y
6 Y
Then, there are multiple data tables along these lines:
Table1
InstanceID field1 reference_field2
1 John 5
2 Sally NULL
3 Fred 6
4 Joe NULL
Table2
InstanceID field3
5 1
6 1
Table3
InstanceID fieldID field4
5 1 Howard
5 2 James
6 2 Betty
Please note that reference_field2 in Table1 contains a reference to another instance.
Field3 in Table2 is a bit more complicated. It contains a fieldID for Table 3.
What I need is a query that will get me a list as follows:
InstanceID field1 field4
1 John Howard
2 Sally
3 Fred
The problem is, in the query I currently have, I do not get Fred because there is no entry in Table3 for fieldID 1 and InstanceID 6. So, the very best list I have been able to get thus far is
InstanceID field1 field4
1 John Howard
2 Sally
In essence, if there is an entry in Table1 for Field 2, and there is not an entry in Table 3 that has the instanceID contained in field2 and the field ID contained in field3, I don't get the data from field1.
I have looked at joins till I'm blue in the face, and I can't see a way to handle the case when table3 has no entry.
LEFT JOIN...
SELECT a.InstanceID, b.field1, d.field4
FROM instances AS a
JOIN Table1 AS b ON a.InstanceID = b.InstanceID
LEFT JOIN Table2 AS c ON b.reference_field2 = c.InstanceID
LEFT JOIN Table3 AS d ON (c.InstanceID = d.InstanceID AND c.field3 = d.fieldId)
WHERE a.active = 'Y'
The two left joins should handle the case where there are no other rows...
It would help if you posted the query you have, because I think you have some mistakes in the table descriptions here, so it's not very clear how are the tables connected.
Anyway, you probably have an inner join in your query (normally written as just JOIN). Replace it with a left outer join (LEFT JOIN). It will not require the right table to contain the row and return NULL instead of the actual value.