Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicate entries in a column [duplicate]

I am writing this query to find duplicate CTN Records in table1. So my thinking is if the CTN_NO appears more than twice or higher , I want it shown in my SELECT * statement output on top.

I tried the following sub-query logic but I need pulls

  SELECT *          table1     WHERE S_IND='Y'      and CTN_NO = (select CTN_NO                       from table1                      where S_IND='Y'                        and count(CTN_NO) < 2); order by 2 
like image 552
anwarma Avatar asked Dec 23 '10 21:12

anwarma


People also ask

Can you find duplicate entries in Excel?

Head up to the Home tab and locate the Styles section. Now click on Conditional Formatting to open a dropdown menu. 3. Go to Highlight Cell Rules and select the Duplicate values option.

How do I find duplicate values in one column in Excel using Vlookup?

Search & Find Duplicate Data Just create the formula =VLOOKUP(List-1, List-2,True,False) and add it to a third column. The List-1 data will be searched in List-2. If there are any duplicates, then these will be listed in the third column where the formula was placed.

How do I compare two columns in Excel to find duplicates?

Navigate to the "Home" option and select duplicate values in the toolbar. Next, navigate to Conditional Formatting in Excel Option. A new window will appear on the screen with options to select "Duplicate" and "Unique" values. You can compare the two columns with matching values or unique values.


1 Answers

Using:

  SELECT t.ctn_no     FROM YOUR_TABLE t GROUP BY t.ctn_no   HAVING COUNT(t.ctn_no) > 1 

...will show you the ctn_no value(s) that have duplicates in your table. Adding criteria to the WHERE will allow you to further tune what duplicates there are:

  SELECT t.ctn_no     FROM YOUR_TABLE t    WHERE t.s_ind = 'Y' GROUP BY t.ctn_no   HAVING COUNT(t.ctn_no) > 1 

If you want to see the other column values associated with the duplicate, you'll want to use a self join:

SELECT x.*   FROM YOUR_TABLE x   JOIN (SELECT t.ctn_no           FROM YOUR_TABLE t       GROUP BY t.ctn_no         HAVING COUNT(t.ctn_no) > 1) y ON y.ctn_no = x.ctn_no 
like image 198
OMG Ponies Avatar answered Nov 05 '22 01:11

OMG Ponies