Im looking to compare two different tables row by row to ensure that both the file name and the count associated with the filename match. If either does not match I want to output the two rows that are not matching.
I am Using MySQL as my database for the operation
For example
Expected Actual
FileName Count FileName Count
name1.txt 4 name1.txt 4
name2.txt 7 name2.txt 7
name3.txt 4 name4.txt 4 (invalid filename)
name5.txt 4 name5.txt 5 (invalid count)
Output:
The fOllowing rows did not match:
Expected: Actual:
name3.txt 4 name4.txt 4 (because of filename)
name5.txt 4 name5.txt 5 (because counts are diff)
The purposes of this is for a validation script. There are two tables, one for the expected results, and one for the actual results. The sql is to compare the rows in each table to make sure that they are identical. There are 4 columns in each table. For the tables to be considered "identical" the filename and the count associated to that filename must be found in both tables. If that doesn't happen I would like to output the rows that were not able to find a match.
You want NOT EXISTS :
Table1 :
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.filename = t1.filename AND t1.count = t2.count);
Table2 :
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t2.filename = t1.filename AND t1.count = t2.count);
If you want to collate them as one result set then use union all :
SELECT t1.filename, t1.count, 'table1' as table_name
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2 WHERE t2.filename = t1.filename AND t1.count = t2.count)
UNION ALL
SELECT t2.filename, t2.count, 'table2'
FROM table2 t2
WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t2.filename = t1.filename AND t1.count = t2.count);
Related
I have two tables in a database
Table1
T1ID
T1Value
1
T1V1
2
T1V2
3
T1V3
4
T1V4
Table 2
T2ID
T1FK
T2Value
1
2
T2V1
2
3
T2V2
Please note that Table2 has a foreign key, that references T1ID in Table1. Also note that not all entries in Table1 have corresponding entries in Table2.
Now, I would like to use a SINGLE SQL statement to do both of the two following
If there are corresponding entries in Table2 related to Table1, then run the following statement
SELECT t1.*, t2.T2Value FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.T1ID = t2.T1FK WHERE t1.T1ID = 2
If there aren't any corresponding entries in Table2 related to Table1, then run the following statement (notice that NULL is returned instaed of t2.T2Value)
SELECT t1.*, NULL FROM Table1 AS t1 WHERE t1.T1ID = 4
(Please note that t1.T1ID is supplied by the program, so this value will change. I am just showing you what I need)
How do I combine both the above statements into a single statement?
Thanks.
A simple LEFT JOIN will do what you want. For example:
select t1.*, t2.t2value
from table1 t1
left join table2 t2 on t1.t1id = t2.t1fk
where t1.t1id = <parameter>
For example:
If the parameter is 2, then a related row is found and the query returns the value of it (t2.t2value).
If the parameter is 4, then no related row is found and the query returns NULL in its place.
The title may be misleading but i don't know how to formulate it better.
Suppose i have these rows on my MySQL table:
table1:
id column1 column2
1 1 2
2 1 3
3 2 1
4 3 4
I have written a query to retrieve data that have similar vice-versa values on columns column1 and column2 (id: 1 & id: 3), but I'm having trouble querying over data that don't have similar vice-versa rows (id: 2 & id: 4) or that are sort of unique.
EDIT: The query i've used to get vice-versa rows
SELECT * FROM table1 t1
INNER JOIN table1 t2
ON (t1.column1 = t2.column2 AND t1.column2 = t2.column1);
You could use exists logic here:
SELECT id, column1, column2
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE LEAST(t2.column1, t2.column2) = LEAST(t1.column1, t1.column2) AND
GREATEST(t2.column1, t2.column2) = GREATEST(t1.column1, t1.column2));
A simple solution would be to use your query in the WHERE clause to filter out similar rows:
SELECT *
FROM table1 t1
WHERE (column1, column2) NOT IN
(SELECT t1.column1, t1.column2
FROM table1 t1
INNER JOIN table1 t2
ON t1.column1 = t2.column2 AND t1.column2 = t2.column1)
Demo here
I have a table with id(key).
I was generated a lot of rows.
However some rows where not stored because transactions were not completed.
I have total 44000 of rows: first id is 1 & last id is 44045.
How could I find ids of 44 absent rows?
You can try something like below, to get the rows where no other row with an ID less 1 that the ID of the current row exists and where the ID is not the minimum (which cannot have a predecessor).
SELECT *
FROM elbat t1
WHERE NOT EXISTS (SELECT *
FROM elbat t2
WHERE t2.id = t1.id - 1)
AND t1.id <> (SELECT min(t2.id)
FROM elabt t2);
I have two tables.
table 1
1
2
3
4
table 2
1
2
3
i want to select data from tables
table 1 have all data.
but table 2 may have all data or not.
so
if table 1 data exits in table two data select table 2 data otherwise select table one data.
if table 1 data not exit table 2 select table 1 data.
how can do that?
You need OUTER JOIN:
SELECT t1.ID,
COALESCE(t2.col1, t1.col1) AS col1, -- prefer data from table_2 if exists
COALESCE(t2.col2, t1.col2) AS col2,
-- ...
FROM table_1 t1 -- "table 1 have all data"
LEFT JOIN table_2 t2 -- "table 2 may have all data or not"
ON t1.ID = t2.ID;
Try FULL OUTER JOIN
SELECT t2.col1, t1.col1
FROM
table_1 t1 FULL OUTER JOIN table_2 t2
ON t2.col1 = t1.col1
Or, if full joins are not supported by your database, try emulating them.
Requirement:
We have monthly run process, in which we create Table1 and Table2.
1. Let say if we ran our process in Jan it creates Table2. Since Jan is first run we don't have Table1 created before. so, system should put everything from Table2 into Table1.
2. Now let say if we ran our process in Feb it creates Table2. And our system should check if Table1 exists. If yes and (t2.Run_dt > t1.Insert_dt) then pick all ID's from Table2 which don't exists in Table1 and insert/append those records into Table1.
3. Now let say if we re-ran our process in Feb again it creates Table2. And our system should check if Table1 exists if yes and (t2.Run_dt = t1.Insert_dt) then pick all ID's from Table2 which doesn't exists in Table1 and insert/append those records into Table1.
4. and so on...
I have these two tables;
Table1
ID Price Insert_Dt
----- ------- -------------
345 24.35 01-APR-2015
Table2
ID Price Run_Date
----- ------- -------------
345 24.35 01-MAY-2015
678 15.35 01-MAY-2015
I want to write a query to update table1 on the below given logic.
If Table1.Run_date >= Table2.Insert_Dt
and Table2 - Table1 = records found
then insert new records into Table1
If Table1.Run_date >= Table2.Insert_Dt
and Table2-Table1 = no records found
then do nothing
else do nothing
DECLARE
nCount NUMBER;
mCount NUMBER;
BEGIN
select count(*) into nCount from dba_tables where table_name = Table1;
if ( (nCount>0)
and ( (select max(a.Run_Date) from Table1 a)
> (select max(b.Insert_Date) from Table2 b) ) )
then
create table difference as
select * from Table2 c where c.ID not in(select d.ID from Table2 d)
select count(*) into mCount
from dba_tables
where table_name = 'difference';
if (nCount > 0) then insert /*+ append */ into Table1
select ID,Price,Run_Date
from (select ID,Price,Insert_Date from difference);
end
END;
I think this does what you described, but I have a couple of questions:
insert into table1
with max_run as (
select id, max (insert_dt) as max_date
from table1
group by id
)
select
t2.id, t2.price, t2.run_date
from
table2 t2,
max_run t1
where
t2.id = t1.id (+) and
-- (t1.id is null or t2.run_date >= t1.max_date) changed below, per edit
(t1.id is null or t2.run_date > t1.max_date)
Are you sure you want a >= insert_dt and not a > insert_dt? Having a >= means we keep inserting those records over and over, even though they are not really new
Did you want to insert new records or update existing records with the new info? Your inquiry said you wanted to insert them, and I didn't see any update code in your snippet, so I kept that as-is