Discussion Locked

Duplicate Records in a Query

0 helpful
16 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I've created a query utilizing multiple tables. I am not getting the results as expected. The query is displaying results from the underlying tables and duplicating data. I want it to display one instance of the record and not duplicate.I've looked at the UniqueRecords properties. What step am I not taking?

16 replies

by roc180

Sounds like you are doing a many to many join in your query. To fix the problem look at your join criteria, try to uniquely define the join by specifying multiple fields to join on:
(T1.id=T2.id AND T1.name=T2.name),
or use a GROUP BY clause to flatten your results :
SELECT F1,F2,F3
FROM T1
WHERE F1=Something
GROUP BY F1,F2,F3

If this dosen't solve your problem you cansend me the SQL and your tables and I can take a look at that.

Good Luck

Neal Martinelli

by donq

Your data source is NOT normalized or your Tables aren't properly indexed IF there aren't supposed to be any duplicated? The easiest fix for you is to open your existing query in SQL View and enter the word "DISTINCT" rifht behind "SELECT" as in SELECT DISTINCT . .....

by davram

Why don't you just post the SQL query, also? It would be a lot easier for the rest of us to point you the error.
You probably should provide us with some informations about the technical environment you are using, too.
Thanks.

by ladyskydiver

If your tables are normalized, you not only want to use DISTINCT but double check your join properties between the tables. Are you doing an inner, left/right outer, or full outer join? Depending on the type of join you do will, also, depend on your result set.

by dglundman

Without knowing the environment or the actual query you have constructed it would be hard to offer the right answer. However, the first answer about the way you have constructed your Joins is probably the correct answer. You can use SELECT DISTINCT to force the number of records to be reduced. But, the fastest and better approach would be to fix the Joins. Incorrectly constructed joins can actually return several multiples of records than are in the database.

by dglundman

The SQL construct ? SELECT DISTINCTROW ?. anyfield,anyfield,anyfield, * FROM table1, table2, table3 ?, Will likely produce multiple duplicate records, with or without the DISTINCT, because of the ?*? and the unrestricted request, ie no WHERE clauseand/or no JOINs.

My first suggestion is to only use ?*? if you really mean it. In Ms SQL, if you have two tables; one with 4 records and the other with 3 records, a ?SELECT * FROM table1,table2? will likely return 12 records or rows.

I am guessing from the sample you sent that you are using Access or some other SQL Compliant Object Query Language. SQL provides a number of runtime methods of joining related tables. My preference is using the WHERE clause.

With Ms SQL 7.0 & 2000, Enterprise Manager you get the SQL Query Analyzer Tool which is useful in optimizing Queries.

The clause might be something like ? SELECT DISTINCT * FROM table1,table2,table3 WHERE table1.columnA = table2.columnC AND table2.columnH =* table3.columnK ? This is a one-to-one relationship between table1 and table2 and a one to many relationship between table2 and table3. The possibilities are endless.

Of course Ms SQL Query Tools should warn you if you try to construct a circle Join, orother illegal Join, but they won?t tell you about incomplete Join Paths. I like to try a Query in the Analyzer before I place it in any code or stored procedure.

An example of an incomplete Join is where you define the JOIN for the first two tables but not the third. That would cause all the rows (records) from the first two tables to be repeated for every row from the third table.

[see full response in email]

This discussion is closed to new replies.