1

I have a table with 4 entries.

    CREATE TABLE tab( 
    name Text 
                     ); 

    INSERT INTO "tab" VALUES('Intertek');
    INSERT INTO "tab" VALUES('Pntertek');
    INSERT INTO "tab" VALUES('Ontertek');
    INSERT INTO "tab" VALUES('ZTPay');

Pntertek & Ontertek are fuzzy duplicates of the correctly spelt Intertek. I wish to create a list consisting of fuzzy duplicates and the correctly spelt names. However, I don't want the list to contain the correctly spelt name if there is no fuzzy duplicate found by the LIKE search.

The following line tells me how many entries match my fuzzy search criteria:

    SELECT COUNT(name) 
    FROM tab 
    WHERE name LIKE '%ntertek' ;

    SELECT COUNT(name) 
    FROM tab 
    WHERE name LIKE '%TPay' ;

This works fine and gives 3 and 1 respectively.

I know this next part is wrong but it expresses what I want to happen:

    SELECT name 
    FROM tab 
    WHERE name LIKE '%ntertek'
    GROUP BY name 
    HAVING COUNT(name) FROM tab WHERE name LIKE '%ntertek' > 1 ; 

    SELECT name 
    FROM tab 
    WHERE name LIKE '%TPay'
    GROUP BY name 
    HAVING COUNT(name) FROM tab WHERE name LIKE '%TPay' > 1 ; 

In my mind this should create the following list: Intertek, Ontertek, Entertek.

But I get a near "FROM": syntax error.

I'm somewhat of a novice with sql and programming in general so any help would be greatly appreciated.

Thanks in advance for any help.

4

2 回答 2

1

If you use GROUP BY name, you will get a separate group for every distinct name, and then you will not be able to count similar names.

You should use a subquery:

SELECT name
FROM tab
WHERE name LIKE '%ntertek'
  AND (SELECT COUNT(*)
       FROM tab
       WHERE name LIKE '%ntertek') > 1
于 2013-07-09T06:47:17.207 回答
0

I believe you are looking for SELECT DISTINCT

SELECT DISTINCT name
FROM tab
WHERE name LIKE '%ntertek';
于 2013-07-08T23:06:36.343 回答