(VFP) - How to extract duplicate records from a table ?
How to extract duplicate records from a table ?
By duplicate, I do not mean the replica of one record in another. For example, a sigle field such as Account_Id field may be existing twice or more number of times in a table, while the rest of the records content could be different. Or simply two fields concatenated may exist more than once in the same table.
We can quickly obtain a list of such field values (let us call it duplicated records) with the code....
SELECT account_id FROM customer GROUP BY account_id ;
HAVING COUNT(account_id) > 1
OR
SELECT first_name + last_name AS cust_name ;
FROM customer GROUP BY first_name + last_name ;
HAVING COUNT(first_name + last_name) > 1
The above only picks up the field values which has duplicate records, while all the duplicated records are not listed. With the following code, we can get the full list of duplicated records.
SELECT * FROM customer WHERE account_id IN ;
(SELECT account_id FROM customer GROUP BY account_id ;
HAVING COUNT(account_id) > 1)
OR
SELECT * FROM myTable WHERE myField IN ;
(SELECT myField FROM myTable ;
GROUP by myField HAVING COUNT(myField) > 1);
INTO CURSOR myDuplicates
However if we need to extract all the unique records from a table having replica of one record in one or more other records can be easily obtained by the code..
SELECT * FROM myTable UNION SELECT * FROM myTable
The result will only have unique records.
By duplicate, I do not mean the replica of one record in another. For example, a sigle field such as Account_Id field may be existing twice or more number of times in a table, while the rest of the records content could be different. Or simply two fields concatenated may exist more than once in the same table.
We can quickly obtain a list of such field values (let us call it duplicated records) with the code....
SELECT account_id FROM customer GROUP BY account_id ;
HAVING COUNT(account_id) > 1
OR
SELECT first_name + last_name AS cust_name ;
FROM customer GROUP BY first_name + last_name ;
HAVING COUNT(first_name + last_name) > 1
The above only picks up the field values which has duplicate records, while all the duplicated records are not listed. With the following code, we can get the full list of duplicated records.
SELECT * FROM customer WHERE account_id IN ;
(SELECT account_id FROM customer GROUP BY account_id ;
HAVING COUNT(account_id) > 1)
OR
SELECT * FROM myTable WHERE myField IN ;
(SELECT myField FROM myTable ;
GROUP by myField HAVING COUNT(myField) > 1);
INTO CURSOR myDuplicates
However if we need to extract all the unique records from a table having replica of one record in one or more other records can be easily obtained by the code..
SELECT * FROM myTable UNION SELECT * FROM myTable
The result will only have unique records.
Comments