Finding non-matching data in two tables as per columns and arranging column data as row by row - sql

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+

use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

Related

How to find out if a row of one table exists in the values of at least one row of another table?

I have two SQL tables, example below:
Table 1 (column types varchar, integer, numeric)
A
B
C
D
A007
22
14.02
_Z 1
A008
36
15.06
_Z 1
Table 2 (column types varchar)
A
B
C
D
A009,A010,A011
33,35,36
16.06,17.06
_Z 1,_Z 2
A003,A007,A009
14,22,85
13.01,17.05,14.02
_Z 1
Is there a way to compare individual rows of the first table with the rows of the second table and find out which row of the first table does not occur in the values of any row of the second table?
As can be seen, the first row of table 1 occurs in the values of the second row of table 2.
However, the second row of table 1 does not occur in the values of the rows of table 2, therefore the desired output is row 2 of table 1.
Desired output table:
A
B
C
D
A008
36
15.06
_Z 1
What I have tried so far:
My solution was to create a table containing all possible combinations of column values for each row of the second table (with the same column data types as the columns of the first table) and then use SELECT * FROM TABLE1 EXCEPT SELECT * FROM TABLE2 to get the difference rows.
The solution worked (for relatively small tables) but I am currently in a situation where generating all combinations of column values for each row of the second table (which in my case has 500 rows) results in a table containing millions of rows, so I am looking for another solution, where I can use the original table with 500 rows.
Thank you in advance for any possible answer, preferably one that could also work in the IBM DB2 database.
We can use a LIKE trick here along with string concatenation:
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE ',' || t2.A || ',' LIKE '%,' || t1.A || ',%'
);
Note that it would be a preferable table design for Table2 to not store CSV values in this way. Instead, get every A value onto a separate row.

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

Microsoft Access Query - Merging two queries into one

Using the query wizard, I built two different queries doing similar functionalities that I am trying to combine into one query. I have two tables (same structure) that I am matching to find duplicates:
Query #1 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 7 to Table 2 Column 7
If all of those columns from Table 1 match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7 which will show the duplicates I am looking for).
Query #2 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 8 to Table 2 Column 8
My second query has the same 3 columns, except the last column is different.
If all of those columns from Table 1, match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7/8 which will show the duplicates I am looking for).
What I am trying to do:
Add an OR statement for the query to show both duplicates on matches for Columns 8 and Columns 7. Such as if Table 1 Column 7 matches Table 2 Column 7 OR Table 1 Column 8 matches Table 2 Column 8, show the duplicates.
Would this require a UNION query?
Here is the query for one of them:
SELECT TABLE1.COLUMN_3, TABLE1.COLUMN_4, TABLE1. COLUMN_7,
TABLE2.COLUMN_7, TABLE1.COLUMN_5
FROM TABLE1
LEFT JOIN TABLE2
ON (TABLE1.COLUMN_7 = TABLE2.COLUMN_7)
AND (TABLE1.COLUMN_3 = TABLE2.COLUMN_3)
AND (TABLE1.COLUMN_4 = TABLE2.COLUMN_4)
AND (TABLE1.COLUMN_5 = TABLE2.COLUMN_5);
The given SQL will not run because of spaces in table and column names.
This example eliminates those spaces.
SELECT
Table1.Column3 , Table2.Column3
,Table1.Column4 , Table2.Column4
,Table1.Column5 , Table2.Column5
,Table1.Column7 , Table2.Column7
,Table1.Column8 , Table2.Column8
From Table1
Left Join Table2
On
( Table1.Column3 = Table2.Column3
AND Table1.Column4 = Table2.Column4
AND Table1.Column5 = Table2.Column5
AND ( Table1.Column7 = Table2.Column7
OR Table1.Column8 = Table2.Column8
)
)
Create query 3; add Q1 and Q2 - join them on F7. This will result in all matches of that field.
Create query 4; add Q1 and Q2 - join them on F8. This will result in all matches of that field.
You now have 2 data sets with those matches (you call duplicates). Then the decision is how to present/display. If you need them in a single record set - write those into a single common temp table. But otherwise you can easily present them together as sub reports/forms.

SQL/T-SQL Substring LEFT or Right doesn't appear to resolve

I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);

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.