Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Duplicate Values

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2009-07-10 : 08:44:50
Hi

I have a query that returns duplicate values from one of the columns and I am trying to figure out how to do this without distinct. There is a call to a function to return these values in the select statement but how can you say for a certain join only return one value of each match?

I joined the two tables (5 and 2) on their own and I can see the duplicate value appear after I join them. The parent child is table2 - table 5 and duplicate values are in table 5.



SELECT         dbo.Table1.Tab1Col1, 
dbo.Table2.Tab2Col1,
dbo.ShowTableNumber(dbo.Table2.Number) AS Number,
dbo.Table2.Tab2col2,
dbo.Table2.Tab2col3,
dbo.Table2.Tab2Col4,
dbo.Table2.Tab2Col5,
dbo.Table2.Tab2Col6,
dbo.Table4.Tab4Col1,
dbo.Table5.Tab5Col1,
dbo.Table6.Tab6Col1,
dbo.Table6.Tab6Col2
FROM dbo.Table6 INNER JOIN
dbo.Table5 ON dbo.Table6.Table6ID = dbo.Table5.Table6 RIGHT OUTER JOIN
dbo.Table2 INNER JOIN
dbo.Table4 ON dbo.Table2.pk = dbo.Table4.fk INNER JOIN
dbo.Table1 ON dbo.Table2.pk = dbo.Table1.FK ON dbo.Table5.Number = dbo.Table2.Number AND
dbo.Table5.pk = dbo.Table2.fk


Just wondering if there is a way to do this without distinct. Distinct does not work here, which I cannot explain to myself exactly why but I don't think it is right to use it here.

Thanks for any pointers

G

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-10 : 11:13:11
apply group by over fields you want to get distinct on and then apply aggregates over other fields
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2009-07-11 : 04:10:19
quote:

how can you say for a certain join only return one value of each match

By providing a join condition that is unique in both tables.
quote:

Distinct does not work here, which I cannot explain to myself exactly why


It sounds like you don't understand your data well enough to answer your question. There is no fairy dust. You need to understand your problem and define it clearly before you or anyone else can begin to address it.
Go to Top of Page
   

- Advertisement -