SQL Help - SQL sub-query 
Author Message
 SQL Help - SQL sub-query

I have the need to mark records in a table as dupplicated via SQL

I came up with the below SQL which just tells me HOW many dupplicated
records I have in the table!(:

SELECT MIN(field0), MIN(field1), Cnt(*) FROM mytable
GROUP BY field0, field1 HAVING Count(field0) > 1
AND Count(field1) > 1

what I envision is that I should somehow use this query as a sub-query
to another one which SETs mytable.dupplicated as .T., but how?
--
Vu Nguyen

// http://www.*-*-*.com/


///////////////////////////////////////////////////////////////
//Disclaimers:  my opinions do not reflect that
//of my employer, USRobotics



Mon, 06 Dec 1999 03:00:00 GMT  
 SQL Help - SQL sub-query

You can do this with a subquery, as you suggested.

update mytable
set duplicated = t
where (key) in (
        select (key)
        from mytable
        group by key
        having count(*) > 1)

The tricky bit is that you appear to have a multipart primary key,
field0 and field1. In order to update the table in this way, you must
substitute a concatenated or hashed value of the two (or n) columns
for the comparison, e.g. field0 concat field1. If the fields are
numeric, you may need to cast them as strings, or come up with a
hashing formula to create unique values for the composite.

Ugleee SQL. ;-) At least you don't have a correlated subquery - should
be fairly fast, if you don't have a high percentage of duplicates.

Next question: why isn't there a unique index on the key columns
before insertion, to reject duplicates?

Quote:

>I have the need to mark records in a table as dupplicated via SQL

>I came up with the below SQL which just tells me HOW many dupplicated
>records I have in the table!(:

>SELECT MIN(field0), MIN(field1), Cnt(*) FROM mytable
>GROUP BY field0, field1 HAVING Count(field0) > 1
>AND Count(field1) > 1

>what I envision is that I should somehow use this query as a sub-query
>to another one which SETs mytable.dupplicated as .T., but how?
>--
>Vu Nguyen

>//http://www.geopages.com/SiliconValley/2214/


>///////////////////////////////////////////////////////////////
>//Disclaimers:  my opinions do not reflect that
>//of my employer, USRobotics

Paul Horn
---------
Return email: remove the X from the address above


Tue, 07 Dec 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. SQL Query - MAX, Grouping and Sub query

2. Using Sub Query in Sql Query - Crystal Report 5.0

3. SQL Sub-queries

4. Q: Pass a SQL query from VB to crystal report sub-report

5. Formula in a SQL Sub Query

6. How to change SQL query in Sub-report

7. Sub SQL Query

8. CR SQL designer bug??? with SQL subquery

9. Converting dynamic query by example SQL to RDO based SQL Stored Procedure

10. Q: MS SQL queries -> ANSI SQL

11. SQL queries on sql results

12. Q: MS SQL queries -> ANSI SQL

 

 
Powered by phpBB® Forum Software