Filter duplicate records based on multiple columns from a table, then delete records from the duplicate records based on ranking [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 hours ago.
Improve this question
SELECT
t.UniqueID, t.qty, t.name1, t.Surname, t.City,t.State,t.Specialty, t.Languages,t.Company, t.Email
FROM (
SELECT
s.name1, s.Surname, s.City,s.State,s.Specialty, s.Languages,s.Company, s.Email,s.UniqueID
, COUNT(*) OVER (PARTITION BY s.name1, s.Surname, s.City,s.State) AS qty
FROM [dbo].[Diverse_Full_Dataset] s
) t
WHERE
t.qty > 1
The above query gives the duplicate records like this with Speciality, Languages, Company and Email columns included from the table.
https://i.stack.imgur.com/YL2qx.png
The query gives about 113,721 records.
First I need to place weight on data fields for Speciality, LANGUAGE, COMPANY and EMAIL:
I want to write a query where if I have a record greater than 1, say 4, then I want to check for all 4 of the duplicate rows whether Speciality is null; if not null then delete all duplicate records; now if Speciality is null then check next whether LANGUAGE is null; if not null then delete all duplicate records; now if LANGUAGE is null then check whether Company is null; if not null then delete all duplicate records; now if Company is NULL then check whether Email is null. It sets a priority on these four columns
What is the query for the above scenario?

Related

oracle sql 12 how to display the number of current account is not displaying , any suggestions please?the query and table below [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
select distinct b.brid,
(select count(1) from account a
where a.brid= b.brid
and a.acctype='current'
) as num_accounts,
b.baddress.street, b.baddress.city, b.baddress.p_code
from branch b;
the table should display the number of current account,but nothing is showing
Query you posted doesn't make sense. What is b.baddress.street? The way you put it,
b is user
baddress is table
street is column
but what is b.brid, then?
You posted screenshot of ... what? Desired result? Can't be as it displays 2 columns, while query returns 5 of them.
Anyway: this is how it might be done. Try to adjust it to your table(s). Mind letter case (is it really 'current'? Maybe 'CURRENT'? Or ...?)
SELECT b.brid,
COUNT (*) num_accounts,
b.street,
b.city,
b.p_code
FROM branch b JOIN account a ON a.brid = b.brid
WHERE a.acctype = 'current'
GROUP BY b.brid,
b.street,
b.city,
b.p_code;
You can use a correlated subquery for this purpose:
select b.*, -- or whatever columns you want
(select count(1)
from account a
where a.brid = b.brid and a.acctype = 'current'
) as num_accounts
from branch b;
In other words, you appear to have an error in how your are referencing columns in b, but the actual part of the query that does the count is correct.

I need to select all records from the table without the record which I selected by subquery, but it return all records again [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Select * from Loan
where EXISTS (Select * from Loan where Loan_No != 'A1234')
if you want to select the records with loan_no != 'A1234' just use this :
select * from loan where loan_no != 'A1234'
and if you want to select records without records that you selected in subquery use this:
select * from loan where loan_no not in (select loan_no from loan where XXX)
Huh? Why aren't you just using this?
select l.*
from loan l
where loan_no <> 'A1234';
Your query either returns ALL rows in the table or NONE of them. Why? Because exists returns true if the subquery returns any rows and false otherwise. There is no correlation to the outer query, so the condition is the same for all rows.

get relation data from one table in oracle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table, let's called it data_employee. It contains:
(employee_name, username, department, division, group, job)
and the data is:
(john_doe, john.doe, **it_sec**, null, it, **network_adm**)
(smith_jaeger, smith.jaeger, **it_sec**, null, it, **Department_Head**)
I want to query username's superior, which is:
if username has department,
then filter their department (e.x: it_sec) and the job department_head
if username has not department,
then filter their division like above and so on deligate until group head
I've tried subquery but sometimes they return multiple data, because I have sub group head
and the data also written group head
so, for an example: the result is:
when I want to find john_doe's department head, the result is
smith_jaeger
Consider this query:
select de1.employee_name, de2.employee_name
from data_employee de1
join data_employee de2
on ((de1.department = de2.department) and (de1.job <> 'Department_Head') and (de2.job = 'Department_Head')) or
((de1.department is null) and (de1.division = de2.division) and (de2.job <> 'Department_Head'))
The first criteria is before the or, the second criteria is after the or. In the first criteria it's important to specify that an employee is department head and the other is not to avoid having results saying that the department head is his/her own boss. With the second criteria we did not exclude duplicates, because if there could be separate departments with the same division (which should not be the case, but we cannot exclude inconsistency, especially if these are textual data), then the multiple bosses of the separate departments of the same division will appear in the results. Also, if there are multiple department heads, that's a problem as well.

Multiple WHERE Query to eliminate data? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need help creating a large query to select a subset of data.
I need to filter out certain information before I can do my analysis. First I select any call which reached my programs. Then I need to filter out all of the fake/test calls. Then I need to eliminate the ineligible calls which is dependent on a few factors like if the Owner or Sender column is a specific designation, or if the Creator column is a specific designation ONLY IF the call type is one of two options. It is super complicated for me.. hopefully someone can help.
SELECT * FROM Table1
WHERE CustomerID IN (SELECT CustomerID FROM Table1
WHERE SiteID
IN ('Site1', 'Site2'))
WHERE CustomerID <> (108 different values)
AND OwnerID <> (A)
AND SenderID <> (A)
AND CreatorID <> (A) but ONLY if CallType <> CallType1 or CallType2;
SELECT *
FROM Table1
WHERE CustomerID IN (
SELECT CustomerID
FROM Table1
WHERE SiteID IN ('Site1', 'Site2'))
AND CustomerID not in (108 different values)
AND OwnerID <> (A)
AND SenderID <> (A)
AND not ( CreatorID = (A) and
CallType in ( CallType1, CallType2 ) )

I need to print those entries which are being repeated based on ID column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
have a table EMP having two columns ID, Name
EMP contains lots of entries, obviously, repeated entries.
I need to print those entries which are being repeated, but, i don't have to look on NAME column, on the basis of id only i want to print the repeated entries.
Please help me out. Thanks in advance.
To display all the repeated ids:
SELECT ID
FROM EMP
GROUP BY ID
HAVING COUNT(*) > 1
To also print the names:
SELECT ID, NAME
FROM EMP
WHERE ID IN
(
SELECT ID
FROM EMP
GROUP BY ID
HAVING COUNT(*) > 1
)