Delete duplicate values [duplicate] - sql

This question already has answers here:
Oracle - deleting duplicates
(2 answers)
How to delete duplicate records from a table in oracle
(5 answers)
Removing duplicate rows from table in Oracle
(24 answers)
Closed 2 years ago.
I hava a table emp, I wish to delete the duplicate values.
The subquery is good.
SELECT ename
FROM emp
GROUP BY ename
HAVING Count(empno) > 1
But when I do the delete operation its deleting all the lines. I don't understand what mistake I'm doing.
DELETE FROM emp
WHERE ename IN (SELECT ename
FROM emp
GROUP BY ename
HAVING Count(empno) > 1);

It deletes all ENAMEs, not only duplicates (i.e. it doesn't leave one row). Try something like
Table contents at beginning:
SQL> select * From temp order by id;
ID ENAME
---------- ----------
1 Little
2 Little --> duplicate ENAME
3 Foot
Deleting:
SQL> delete from temp a
2 where a.rowid > (select min(b.rowid)
3 from temp b
4 where b.ename = a.ename
5 );
1 row deleted.
Result;
SQL> select * From temp order by id;
ID ENAME
---------- ----------
1 Little
3 Foot
SQL>

Related

How to intersect 5 sql queries that do not belong to same table and have millions of data as output for each query

How to intersect 5 SQL queries that do not belong to same table and have millions of data as output for each query?
I want to find out data that is present in all the five queries output.
You can intersect them, as you said (all statements have to have equal number of columns and they have to match in datatype), e.g.
SQL> select empno, ename from emp
2 intersect
3 select deptno, loc from dept;
no rows selected
SQL>
Or, you can use each query as a CTE and then join them (result will return rows that match):
SQL> with
2 q1 (valn, valc) as
3 (select empno, ename from emp),
4 q2 (valn, valc) as
5 (select deptno, loc from dept)
6 --
7 select a.valn
8 from q1 a join q2 b on a.valn = b.valn and a.valc = b.valc;
no rows selected
SQL>
Millions of rows? It'll probably take time to get the result, I presume.

What is mean the condition COUNT(1) = 1 in oracle sql

I have two table and write select:
SELECT A.code, B.account, A.ano
FROM atable A, dtable B
where A.ano=B.dno
group by A.code, B.account, A.ano
having count(1)=1;
if I add condition having count(1)=1 I didn't get any result, why?
Query groups data per columns listed in the GROUP BY clause. HAVING restricts result set to those whose count (for those grouped columns) equals 1.
If query returns nothing, it means that there's not at least one combination of those columns that has only one row as a result.
For example (based on Scott's EMP table):
SQL> select job, count(*) from emp
2 group by job;
JOB COUNT(*)
--------- ----------
CLERK 4
SALESMAN 4
PRESIDENT 1 --> only one person is a president
MANAGER 3
ANALYST 2
Your query:
SQL> select job
2 from emp
3 group by job
4 having count(*) = 1;
JOB
---------
PRESIDENT
Let's set someone else to be a president:
SQL> update emp set job = 'PRESIDENT' where ename = 'ADAMS';
1 row updated.
The query doesn't return anything now (as there are 2 presidents):
SQL> select job
2 from emp
3 group by job
4 having count(*) = 1;
no rows selected
SQL>
As of count(1): no point in it, use count(*) instead.

To delete duplicate rows in Oracle SQL [duplicate]

This question already has answers here:
Removing duplicate rows from table in Oracle
(24 answers)
Closed 3 years ago.
I've used this code to select the duplicate rows, it went right,
SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT>1;
But it doesn't help me in deleting the same selected rows....
DELETE emp
WHERE name IN ( SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT >1);
Remove the second column from the inner SELECT, something like this:
DELETE
FROM emp
WHERE name IN( SELECT name FROM emp GROPU BY name HAVING COUNT(name) > 1)

Get Unique column value along with other. Columns when using inner join

I have following query and I need unique value for column
Select unique(t.id), log.*
from tableA log
inner join tableT t on t.id1=log.id1
where log.time>=(somedate)
and log.time<(somedate)
and ref-id=20
and ref-id=30
and t.id not in (select unique t.id
from tableA log
inner join tableT t on t.id1=log.id1
where log.time>=(somedate)
and log.time<(somedate)
and ref-id=20
and ref-id=30);
I am not getting unique values for t.id.Can anyone please help ? I am using oracle data base
Remove LOG.* from the SELECT; UNIQUE (as well as DISTINCT) is applied to all columns you select, not only to the one you put into brackets.
[EDIT]
Scott's EMP table contains employees that work in different departments. List of distinct departments is:
SQL> select distinct deptno from emp;
DEPTNO
----------
30
20
10
SQL>
If you include additional columns, such as JOB, the list is changed to
SQL> select distinct deptno, job from emp order by deptno, job;
DEPTNO JOB
---------- ---------
10 CLERK
10 MANAGER
10 PRESIDENT
20 ANALYST
20 CLERK
20 MANAGER
30 CLERK
30 MANAGER
30 SALESMAN
9 rows selected.
SQL>
It is now distinct per DEPTNO and per JOB; you can't get only distinct DEPTNO in such a query.
[EDIT #2: aggregation]
If "distinct" means distinct DEPTNO and the first job for that DEPTNO, then you might need this:
SQL> select deptno, min(job) first_job from emp
2 group by deptno
3 order by deptno;
DEPTNO FIRST_JOB
---------- ---------
10 CLERK
20 ANALYST
30 CLERK
SQL>
[EDIT #3, example based on your query]
Select t.id,
min(log.id) log_id,
min(log.time) log_time,
<the rest of LOG table columns goes here, all of them with MIN>
from tableA log
inner join table ...
<the rest of your query goes here>
group by t.id;
How about something like this?
Select t.id, log.*
from tableA log inner join
(select t.*, row_number() over (partition by id1 order by id) as seqnum
from tableT t
) t
on t.id1 = log.id1
where log.time >= (somedate) and
log.time < (somedate) and
t.seqnum = 1
ref_id = 20 and ref_id = 30 -- this is ALWAYS false, but I'm ignoring that
Notes:
I have no idea what the subquery is supposed to be doing.
The conditions on ref_id will always evaluate to FALSE, so that logic is almost certainly not what you intend.
If ref_id is in the transaction table, then the conditions should be moved to the subquery.

Displaying names using conditions [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
I would like to know if I have a certain table, let's say table X
which contains salary and names, how would I display the maximum salary along with corresponding names?
Thank you.
select salary, name
from X
where salary = (select MAX(salary) from X)
Let's see this using EMP table example :
SQL> WITH DATA AS(
2 SELECT MAX(sal) max_sal FROM emp)
3 SELECT ename, sal
4 FROM emp
5 WHERE sal = (SELECT max_sal FROM DATA)
6 /
ENAME SAL
---------- ----------
KING 5000
SQL>