Thursday, March 22, 2012

Duplicate Entries

I have an issue where certain parts of data are repeated several times after i create my query. Without providing my SQL code for now could anyone suggest possibly the main reason(s) for data being duplicated?

Thanks

I reckon its because either.....

a) you have duplicate data in your tables

b) you are doing a JOIN to a table with a one -> many relationship

And as a solution i reckon you could....

a) use DISTINCT in your select statement

Am I close?

|||

I think your very close:

a) Definately not as i recreated another query without all the fields that i originally needed and had NO duplicate items.

b) This maybe it but ill have to investigate it and get back to you.

Based on your answers and solution

1. How can i easily define which tables may have 1-Many relationship?

2. Where would i use the Distinct statement?

Thanks

|||

1. Don't think you can programmatically find out 1->Many relationships. Hopefully you have your relationships defined by foreign keys which should point you in the right direction. Maybe use sp_fkeys to help identify them eg EXEC sp_fkey 'YourTable', 'dbo'

2. SELECT DISTINCT Col1, Col2, Col3.....

Check Books Online for more details.

sql

No comments:

Post a Comment