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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Help

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
Hi All,

I have a sub query. It returns duplicates on the contact number but I would just like the top contact number. How can I do this?

Code:
left join 
	
	(SELECT p.PartyID, sc1.SubCaseRef, pcn.ContactNumber
                        FROM SubCase sc1
                        inner join SubCaseItem sci1 ON sci1.SubCaseID = sc1.SubCaseID
                inner join Venue v1 ON sci1.VenueID = v1.VenueID
                left join Party p ON p.PartyID = v1.PartyID  
                left join PartyContactNumbers pcn ON pcn.PartyID = p.PartyID               
                where v1.VenueID = sci1.VenueID
               group by p.PartyID, sc1.SubCaseRef, pcn.ContactNumber
                ) 
                contactNumJoin ON a.RehabReference = contactNumJoin.SubCaseRef

Thanks,
L

Age is a consequence of experience
 
Hi,

Try something like this:

Code:
with CTE as
(
    SELECT DISTINCT
        p.PartyID, 
        sc1.SubCaseRef, 
        pcn.ContactNumber,
        ROW_NUMBER() OVER(PARTITION BY sc1.SubCaseRef ORDER BY pcn.ContactNumber) as RowNum
    FROM SubCase sc1
    inner join SubCaseItem sci1 ON sci1.SubCaseID = sc1.SubCaseID
    inner join Venue v1 ON sci1.VenueID = v1.VenueID
    left join Party p ON p.PartyID = v1.PartyID  
    left join PartyContactNumbers pcn ON pcn.PartyID = p.PartyID               
    where v1.VenueID = sci1.VenueID
)

select --...
left join CTE 
    ON a.RehabReference = CTE.SubCaseRef and 
       CTE.RowNum = 1

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imoveis-venda-são_paulo-residencial-apartamento[/url]
 
Thanks for your reply.

Ideally I wanted it in one statement.. I came up with. I left the join as it was and in the field select I used max … This removed the second result on the contact number.

select field1,,,, MAX(contactNumJoin.ContactNumber )as ConNumber
inner join ....

Thanks again.



Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top