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);
Related
Well, i'm trying to make a sql request with join but i don't know how to do it.
Here my first table - Table 1
id
postid
user
My second table - Table 2
id
title
Postid and id are the same.
i've done a screenshot of my table 1
As you can see, there are many entries with postid 32. This is totally normal.
I want to do a sql request on this 2 tables.
The results expected have to be like this :
Title of id 31 (from table 1) - 2 (because there are 2 entries with postid 31 in table 2)
Title of id 32 (from table 1) - 23 (because there are 23 entries with postid 32 in table 2)
Someone can help me ?
Try this:
select t1.postid, count(t2.id)
from Tab1 t1 join Tab2 t2
on t1.postid = t2.id
group by t1.postid;
Here the name of the tables are Tab1 and Tab2 and they have aliases t1 and t2.
If you are interested only in the postids of table1 then there is no need for a join:
select postid, count(*)
from table1
group by postid;
If you want to count all the ids of table2 even the ones that are missing in table1 then you need a left join:
select t2.id, count(t1.postid)
from table2 t2 left join table1 t1
on t1.postid = t2.id
group by t2.id;
I have a table named consignment which has some duplicate rows against column "service" where service='CLRC'.
select * from consignment where service='CLRC'
When i select the rows, i have total 2023 rows which includes duplicates.
I wrote the below query to delete the rows but i want to select them first to make sure its deleting the correct records.
When the select runs it returns 64431 records. Is that correct?
select t1.hawb FROM consignment t1
INNER JOIN consignment t2
WHERE
t1.id < t2.id AND
t1.hawb = t2.hawb
and t1.service='CLRC'
If you expect your query to return the number of duplicates then no it is not correct.
The condition t1.id < t2.id will join every id of t1 with all ids from t2 that are greater resulting on more rows or less rows (in the case of only 2 duplicates) and rarely in the expected number.
See the demo.
If you want to see all the duplicates:
select * from consignment t
where t.service = 'CLRC'
and exists (
select 1 from consignment
where service = t.service and id <> t.id and hawb = t.hawb
)
See the demo.
If you want to delete the duplicates and keep only the one ones with the max id for each hawb then:
delete from consignment
where service='CLRC'
and id not in (
select id from (
select max(id) id from consignment
where service='CLRC'
group by hawb
) t
);
See the demo.
Include all the columns in the matching condition except id column, as being primary key :
delete t1
from consignment t1
join consignment t2
where t1.id < t2.id
and t1.hawb = t2.hawb
and t1.col1=t2.col1
and t1.col2=t2.col2
......
and t1.service='CLRC';
Demo
You can check the number of duplicates by
select count(*) from
(
select distinct hawb, col1, col2, service -- (all columns except `id`)
from consignment
) q
check whether this number equals number of deleted records just before commiting the changes.
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);
the sql as follows come from mysql document. it is:
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
The document say It finds all rows in table t1 containing a value that occurs twice in a given column , and doesnot explain the sql.
t1 and t is the same table, so the
count(*) in subquery == select count(*) from t
, isn't it?
count(*) in subquery == select count(*) from t
is wrong. because in mysql you can't use it like that. so you have to run it like that to get result of same id having two rows.
if you want to get count of same occurrence,
SELECT id, name, count(*) AS all_count FROM t1 GROUP BY id HAVING all_count > 1 ORDER BY all_count DESC
And also you can get values as your query like this as well,
select * from t1 where id in ( select id from t1 group by id having count(*) > 1 )
The query contains a correlated subquery in WHERE clause:
SELECT COUNT(*) FROM t1 WHERE t1.id = t.id
It is called correlated because it is related to the main query via t.id. So, this subquery counts the number of records having an id value that is equal to the current id value of the record returned by the main query.
Thus, predicate
(SELECT COUNT(*) FROM t1 WHERE t1.id = t.id) = 2
evaluates to true for any row with an id value that occurs twice in the table.
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
This query goes through each record in t1 and then in the subquery looks into t1 again to see if in this case id is found 2 times (and only 2 times). You can do the same for any other column in t1 (or any table for that matter).
When you would like to see all values that are multiple times in the table, change WHERE 2 = by WHERE 1 <. This will also give you the values that are 3 times, 4 times, etc. in the table.
{
SELECT id,count( * )
FROM
MyTable
group by id
having count( * )>1
}
with this code, you can see the rows which repet more than one,
and you can change this query by yourself
How about using GROUP BY and HAVING:
SELECT id, count(1) as Total FROM MyTable AS t1
GROUP BY t1.id
HAVING Total = 2
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