tags:

views:

39

answers:

1

I have the following code:

FROM CTE_Order cte
    INNER JOIN tblOrders o
       ON cte.OrderId = o.Id
    INNER JOIN tblOrderUnits ou
       ON o.id = ou.OrderId                        
    INNER JOIN tblOrderServiceUnits osu
       ON ou.VMSUnitID = osu.UnitId

When I join the ou I get 2 of the same unit Id's. This make the Inner Join tblOrderServiceUnits return 4 rows with 2 being duplicates. I need it to only return the 2 rows the are different. How do I use a distinct to Inner Join only distinct ou.id?

Sorry for the bad explanation but basically I am jsut trying to see how an INNER JOIN with a distinct subquery would work, If someone could give me an example of that I could figure it out from there.

Thanks!

+3  A: 
INNER JOIN (SELECT DISTINCT * FROM X) Alias
ON Alias.ID = Primary.ID

For your example:

INNER JOIN (SELECT DISTINCT VMSUnitID, OrderId FROM tblOrderUnits) ou
ON o.id = ou.OrderId
treefrog
So If i need to select distinct VMSUnitID in the OrderUnit join how would I do that?
Updated post for clarification.
treefrog