Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

distinct not working for me..

Status
Not open for further replies.

john6767

Technical User
Jun 29, 2006
5
AU
ok i have some sql and i am trying to show all 'people' in my database, the problem arises because i am joing an 'areas' table which has a one to many relationship with my 'people'(in this case tradesmen and the areas is the areas that they service..)

so i am getting as a result a list of all tradesmen and they are repeated for evey area that they service, how do i fix my joins so that it will show one tradesmenID even if there are multiple areas..

heres my sql:
Code:
SELECT distinct t.tradesmenID, t.businessName, tr.tradeName,t.tradeID, a.areaID
FROM tradesmen t INNER JOIN tradesmenarea a on t.tradesmenID = a.tradesmenID INNER JOIN areas ar on a.areaID = ar.areaID INNER JOIN trades tr ON t.tradeID = tr.tradeID
ORDER BY t.businessName ASC
 
Hi

john6767 said:
how do i fix my joins so that it will show one tradesmenID even if there are multiple areas..
Do not display the field from the area table ?

Why did you included the a.areaID field ? And even if you get distinct peoples, what you expect to find in that areaID column ?

I would try to use [tt]group by[/tt] instead of [tt]distinct[/tt] and use an aggregate function on that a.areaID field, for example [tt]min()[/tt].
Code:
[b]select[/b]
t.tradesmenID, t.businessName, tr.tradeName,t.tradeID, min(a.areaID)

[b]from[/b] tradesmen t
[b]inner join[/b] tradesmenarea a [b]using[/b] (tradesmenID)
[b]inner join[/b] areas ar [b]using[/b] (areaID)
[b]inner join[/b] trades tr [b]using[/b] (tradeID)

[b]group by[/b] t.tradesmenID, t.businessName, tr.tradeName,t.tradeID

[b]order by[/b] t.businessName [b]asc[/b]
( I changed the [tt]join[/tt] conditions from [tt]on[/tt] to [tt]using[/tt] just because usually is faster. )

Feherke.
 
thanks, i didn't know about using or group by, great help! yes it doesn't matter what is in the areaID but i need it for a search i'll include in the 'where' clause later..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top